According to this answer on a related question, it's best to make an object explicitly unavailable if you want them to be garbage-collected.
For all practical intents and purposes, does it matter whether that's done with a null
value or an undefined
value?
Shortly put, will both of the below objects (whatever they may have referenced originally) be equally accessible for the garbage collector?
window.foo = null;
window.bar = void 0;
It does not matter what value you assign: it might be null
, undefined
, {}
or 42
.
What matters is that you need to break the connection between a variable and an object in heap.
As soon as an object is not reachable - it's a candidate for being collected, regardless on what the current value the variable that referred to it one day currently holds.
Here is a memory snapshots from a Google Chrome, just for fun:
And the corresponding JSFiddle: http://jsfiddle.net/r9b5taxf/