Possible Duplicate:
Deleting Objects in JavaScript
I have a JS object having a large number of properties. If I want to force the browser to garbage collect this object, do I need to set each of these properties as null or do I need to use the delete operator? What’s the difference between the two?
There is no way to force garbage collection in JavaScript, and you don’t really need to.
x.y = null;anddelete x.y;both eliminatex‘s reference to the former value ofy. The value will be garbage collected when necessary.If you null out a property, it is still considered ‘set’ on the object and will be enumerated. The only time I can think of where you would prefer
deleteis if you were going to enumerate over the properties ofx.Consider the following:
This code will produce the following output: