Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are null and undefined equally valid for explicitly making objects unreachable?

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;
like image 561
Etheryte Avatar asked Sep 28 '22 00:09

Etheryte


1 Answers

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/

like image 142
zerkms Avatar answered Oct 20 '22 12:10

zerkms