5

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;
Community
  • 1
  • 1
Etheryte
  • 24,589
  • 11
  • 71
  • 116

1 Answers1

4

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:

enter image description here

And the corresponding JSFiddle: http://jsfiddle.net/r9b5taxf/

zerkms
  • 249,484
  • 69
  • 436
  • 539