What is JavaScript garbage collection? What’s important for a web programmer to understand about JavaScript garbage collection, in order to write better code?
What is JavaScript garbage collection? What’s important for a web programmer to understand about
Share
Eric Lippert wrote a detailed blog post about this subject a while back (additionally comparing it to VBScript). More accurately, he wrote about JScript, which is Microsoft’s own implementation of ECMAScript, although very similar to JavaScript. I would imagine that you can assume the vast majority of behaviour would be the same for the JavaScript engine of Internet Explorer. Of course, the implementation will vary from browser to browser, though I suspect you could take a number of the common principles and apply them to other browsers.
Quoted from that page:
The main purpose of garbage collection is to allow the programmer not to worry about memory management of the objects they create and use, though of course there’s no avoiding it sometimes – it is always beneficial to have at least a rough idea of how garbage collection works.
Historical note: an earlier revision of the answer had an incorrect reference to the
deleteoperator. In JavaScript thedeleteoperator removes a property from an object, and is wholly different todeletein C/C++.