4

Do we need to unset variables in JavaScript, and why?

For example in PHP it's recommended to unset all temporary variables in all kind of loops to keep memory free and avoid variables duplicates.

But anyways according to php - what's the benefit of unsetting variables? question:

Is it true that unsetting variables doesn't actually decrease the memory consumption during runtime? -Yep.

So should we delete window.some_var;?

Or we should use some_var = null?

Or we should not unset variables to avoid additional CPU cycles?


UPD: Related questions:

How to unset a Javascript variable?

How to remove a property from a javascript object

Community
  • 1
  • 1
dr.dimitru
  • 2,645
  • 1
  • 27
  • 36
  • 1
    It's impossible to delete variables in ECMAScript, you can only delete object properties that are not marked don't delete (i.e. their `[[Configurable]]` attribute is set to false). Global variables are made available as properties of the global (window) object, however if they are created by a declaration (e.g. `var x` or `function x`) they are not deletable. – RobG Oct 17 '13 at 01:47
  • @minitech—I get `function, function` in Firefox, IE throws an error "Can't delete window.hello"), what do you get? See [ECMA-262 §10.5](http://www.ecma-international.org/ecma-262/5.1/#sec-10.5). – RobG Oct 17 '13 at 02:05
  • @RobG: I get `function, undefined` in Firefox. `function, function` in Chrome. *Interesting.* – Ry- Oct 17 '13 at 02:05
  • @RobG: Which part of that are you referring to? – Ry- Oct 17 '13 at 02:07
  • Oh, the Firefox console wraps `window` or something. My bad. – Ry- Oct 17 '13 at 02:09
  • In ECMAScript, declared variables and functions are created with `[[Configurable]]` set to false: [instantiation](http://www.ecma-international.org/ecma-262/5.1/#sec-10.5) -> [CreateMutableBinding](http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1.2.2) `If Boolean argument D is provided and has the value true the new property’s [[Configurable]] attribute is set to true, otherwise it is set to false`. For variable instantiation, the configurable argument isn't passed so it's set to `false`. – RobG Oct 17 '13 at 02:47
  • Go through this link : [http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-between-null-and-undefined][1] [1]: http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-between-null-and-undefined – tilak Oct 17 '13 at 04:35

3 Answers3

4

No, plus in some cases you can't delete variables in the global scope. Variables that are properly declared using the var keyword cannot be deleted (implied global variables can be deleted, on the other hand. Always use the var keyword to declare variables).

Also, javascript engines have this thing called garbage collector that automatically looks for variables that are no longer used or referenced somewhere when your code is 'running'. Once it finds one, it'll shove it off immediately into the Abyss (deletes the variable in the memory)

Cliffmeister
  • 161
  • 10
3

No, it is not necessary to delete variables when you’re done with them. If you have so many enormous, global variables that this is actually a problem, you need to rethink your design.

And yes, it’s also silly in PHP, and no, it has nothing to do with avoiding “additional CPU cycles”.

Specifically:

  • “Unsetting temporary variables in loops”

    They’re temporary, and probably either a) integers or b) references that take up about as much space as an integer. Don’t bother.

  • “Avoiding duplicate variables”

    Again – references. Setting things that are still referenced elsewhere to null is pointless.

If you feel like it’s clutter, that is a good sign that more things should be going out of scope (i.e. use more functions).

In most other cases that you haven’t mentioned, the engine is also smart enough to manage your stuff properly. Do not optimize prematurely.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

David Flanagan answers this quite nicely in his book, JavaScript: The Definitive Guide:

The JavaScript interpreter performs automatic garbage collection for memory management. This means that a program can create objects as needed, and the programmer never needs to worry about destruction or deallocation of those objects. When an object is no longer reachable – when a program no longer has any way to refer to it – the interpreter knows it can never be used again and automatically reclaims the memory it was occupying.

This doesn't mean that memory leaks can't happen in JavaScript. Far from it. Circular (or cyclic) references are a frequent problem in browsers which implement reference counting, as documented here by IBM:

A circular reference is formed when two objects reference each other, giving each object a reference count of 1. In a purely garbage collected system, a circular reference is not a problem: If neither of the objects involved is referenced by any other object, then both are garbage collected. In a reference counting system, however, neither of the objects can be destroyed, because the reference count never reaches zero. In a hybrid system, where both garbage collection and reference counting are being used, leaks occur because the system fails to identify a circular reference. In this case, neither the DOM object nor the JavaScript object is destroyed. Listing 1 shows a circular reference between a JavaScript object and a DOM object.

If you're worried that your website contains a JavaScript memory leak, Google has a tool, aptly named "Leak Finder for JavaScript", which can help you find the cause.

Further reading: What is JavaScript garbage collection?

Community
  • 1
  • 1
James Donnelly
  • 126,410
  • 34
  • 208
  • 218