Reputation: 12562
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!
Upvotes: 0
Views: 587
Reputation: 69713
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.
Upvotes: 1
Reputation: 113382
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).
Upvotes: 2