Tuesday, 13 December 2011

JavaScript Memory Leaks

function foo(node){
node.onclick = function(){
alert(node.id + ' was clicked.';
}
}

That creates a circular reference that is bad. The onclick function has a closure containing the node, and the node has a property (onclick) that contains the function. This is where the big closure scare came from.

Just because the memory increases when you reload a page doesn't indicate a leak. It could indicate IE is caching things. 95% Leaks in IE are created by putting JS Objects on DOM nodes and I never do that.

No comments:

Post a Comment