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?

5
  • "is it really enough to rely on garbage collector to prevent memory leaks?" yes, it is. Commented Jan 17, 2014 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 Commented Jan 17, 2014 at 17:28
  • 1
    Read up here Commented Jan 17, 2014 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. Commented Jan 17, 2014 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. Commented Jan 17, 2014 at 17:32

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, that's an interesting difference, but what about the deletion functionality I was asking about?
@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
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

Comments

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)

3 Comments

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.
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.
"there is no difference between the two really performance wise" OK but how do you reflect @elclarns's answer then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.