1

In my code I use an object and variables inside a function. When in that function I no longer use the object and variables I delete it:

delete myObject;
delete myVar;

Is it good practice?
Does it give speed?

My JavaScript is server-side on smart.joyent.com.

1
  • 1
    you can't delete local variables; strict-mode ES5 will even throw a SyntaxError Commented Jan 1, 2010 at 14:48

4 Answers 4

8

Is it good practice?

No. In fact it is disallowed in the new ECMAScript Fifth Edition ‘strict mode’:

When a delete operator occurs within strict mode code, a SyntaxError is thrown if its UnaryExpression is a direct reference to a variable, function argument, or function name (11.4.1).

If you really must, you can instead =null the variable to free up any object it referenced for garbage collection, but it's almost never going to result in any appreciable improvement in performance.

Deleting properties of long-lived objects to free now-unused referenced objects can be a good idea, especially in the case of breaking DOM cycles to prevent memory leakage in IE. Deleting local variables inside a short-lived function isn't sensible.

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

2 Comments

explicitly setting local variables to null can be useful: because of how naive JS implementations implement closures (they keep all variables of higher scopes alive, and not just the one's referenced in lower scopes - which is only necessary if the inner function contains an eval() with variable argument), it's quite easy to make variables which seem to be short-lived immortal: just assign a function literal to an event handler, and in some JS engines no variable visible from the function can be collected as long as the event handler is around
combine this with IE's inability to collect circular references involving DOM nodes, and you can create an actual leak (ie memory will only be recleimed when you close the browser instance)
1

I'm not sure what engine your server is using, but client-side engines typically employ garbage collection, and I wouldn't be surprised if your server environment does as well. Unless you have an extremely particular situation, most attempts of manual cleanup are probably going to be on par with (or working against) the garbage collector.

In summary, I would bet you see absolutely no difference in performance. This is a good example of micro optimization.

Comments

1

And also instead of using delete method, It seems that Firefox 3 allows script to invoke garbage collector with:

Components.utils.forceGC

See https://developer.mozilla.org/En/Components.utils.forceGC

Other browsers may provide similar functions too. (except IE, i don't think it provides.)

2 Comments

which doesn't help if there's still a live reference to the object around
Yes and it should not delete an object reference which is not out of scope. In Java garbage collector does not remove objects unless it needs more memory. I don't know how mozilla, opera, ie, chrome designed their garbage collector for Javascript. But it would be similar. So instead of delete make the objects null and invoke garbage collector.
0

It is not generally considered necessary but in extreme, high memory situations is may be something you should look into. I do feel that its use prior to profiling and performance testing to be premature optimization.

Comments

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.