2

How shall I delete objects in Javascript to properly destroy them (call destructors if there are any?) and prevent memory leaks? I've seen two ways:

delete obj;

and

obj = null;

But even after reading this I have not understood what is really the difference and what should I use.

Also, I guess there are not real destructors in Javascript, but in case of complex relationship of multiple objects is it really enough to rely on garbage collector to prevent memory leaks?

Community
  • 1
  • 1
Tomas
  • 57,621
  • 49
  • 238
  • 373
  • "is it really enough to rely on garbage collector to prevent memory leaks?" yes, it is. – dandavis Jan 17 '14 at 17:28
  • *in case of complex relationship of multiple objects is it really enough to rely on garbage collector to prevent memory leaks?* yes modern javascript interpreters have no problem at all with (circular) references – Fabrizio Calderan Jan 17 '14 at 17:28
  • 1
    [Read up here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete) – Nicholas Hazel Jan 17 '14 at 17:29
  • But my question is WHY? You should rely on scope of the variables so the GC can make its job. It should not be necessary to be deleting objects. I guess there must be special cases, but it is hard to think about any. – Raul Guiu Jan 17 '14 at 17:31
  • you can use delete to trim down global object's keys. yes, sometimes you actually want global objects. delete is also nice for cleaning up data objects before serialization. at least set stuff to undefined instead of null, which is mostly a DOM stand-in for missing properties. – dandavis Jan 17 '14 at 17:32

3 Answers3

4

One major difference between the two is the value of obj after the operation. Consider

x = 42
x = null
console.log(x)  // prints: null
delete x;
console.log(x)  // prints: undefined

Assigning null is giving a new value to an existing property. It will still exist after the operation. When you delete a property it is removed entirely.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • OK, that's an interesting difference, but what about the deletion functionality I was asking about? – Tomas Jan 17 '14 at 17:36
  • @Tomas you can't really delete objects in javascript. All you can do is remove all references and let the garbage collector pick them up for you later on – JaredPar Jan 17 '14 at 17:38
3

Google has something to say about delete:

Prefer this.foo = null

Foo.prototype.dispose = function() {
  this.property_ = null;
};

Instead of:

Foo.prototype.dispose = function() {
  delete this.property_;
};

In modern JavaScript engines, changing the number of properties on an object is much slower than reassigning the values. The delete keyword should be avoided except when it is necessary to remove a property from an object's iterated list of keys, or to change the result of if (key in obj).

Edit

Some performance test: http://jsperf.com/delete-vs-nullify

Vitor Canova
  • 3,918
  • 5
  • 31
  • 55
elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

The article you have linked says that delete only deletes a reference, therefore there is no difference between the two really performance wise. The only theoretical difference is the garbage collector will be more efficient because it's been explicitly told what to do, but unless you have incredibly large complex objects with many references in hard to reach locations it shouldn't be an issue.

The other answer on the question explains an actual usecase that highlights a difference, ie. removing a property from an object. Accessing it would then give you undefined rather than null.

Here's an example of the latter: http://jsfiddle.net/YPSqM/

function cat() { this.name = 'mittens'; };

var myCat = new cat();

alert(myCat.name); // 'mittens'

myCat.name = null;

alert(myCat.name === null); // 'true' as it is null but not undefined

delete myCat.name;

alert(myCat.name === undefined); // 'true' as it is undefined (but not null)
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
  • um, maybe it's just me and my lack of coffee, but i don't see how they are the same at all. if there were other refs to the object, delete would do nothing to ram use. setting a var to null does nothing to the refs, besides updating the value pointed to. deleting lexical variables is not even universally supported; delete is really designed for removing object properties. – dandavis Jan 17 '14 at 17:31
  • OP was asking about performance, so in terms of performance, it doesn't make any difference as in neither case is the actual object being removed, only the reference is. – NibblyPig Jan 17 '14 at 17:36
  • *"there is no difference between the two really performance wise"* OK but how do you reflect @elclarns's answer then? – Tomas Apr 08 '14 at 16:08