0

When you use Node.removeChild() it return to you the removed node. Then you can append it again on your DOM, if need. So, logically, it stay on memory to make it possible. You can check it on http://jsfiddle.net/6QtjD/.

My doubt is: if I remove too many nodes, setting the removed node to a var locally, it'll stay on memory until page change or until the end of scope?

For instance: if I create 1000 nodes and remove all, every 0.1 second. In 10 seconds my page will use the space of 100.000 nodes on memory?

function create1000() {
    var div, i;

    for(i=0; i<1000; i++) {
        div = document.createElement("div");
        div.innerText = i;
        document.body.appendChild(div);
        document.body.removeChild(div); 
    }
}

for(var i=0; i<10000; i+=100)
    setTimeout(create1000, i);

Thanks!

1
  • 3
    You are not keeping a reference to the elements, so they will be garbage collected whenever the garbage collector runs and will stay in memory until then. Commented Sep 3, 2012 at 0:12

2 Answers 2

2

No reason why it should, because you don't hold onto it, so it should be collectible.

There have certainly been all manner of leaks in different browsers' js implementations over the years, but I wouldn't worry about this case unless I saw clear evidence (and the get that clear evidence, I'd try it myself).

Sign up to request clarification or add additional context in comments.

Comments

1

When you don't even catch the return value of document.body.removeChild() you can be 99.9% sure that the javascript implementation will discard it and not keep it in memory even though there is guaranteed no way to access it. The 0.1% doubt are because this is an internal detail of the browsers javascript engine, and you can't be sure that there exists no browser on the planet which leaks memory in this situation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.