I often see in third party JavaScript code that after:
var el = document.getElementById(elementId);
object is often nulled and comment along this operation says that it is done for IE:
el = null; // IE
What’s the real purpose? Any resource on that?
By nixing a reference they break the corresponding cyclic dependency between the DOM object and JavaScript objects, which are controlled by different sub-systems in older IE (thus being impossible to be garbage-collected).
For example:
The JavaScript subsystem has now a reference to the
elelement, and the DOM subsystem (theelelement) has a reference to the JavaScript object (the function plus what it closes in).You don’t have to worry, though, if you add the listeners via
addEventListener.To read more about common memory leak pitfalls, see http://www.ibm.com/developerworks/web/library/wa-memleak/.