A couple of things you're forgetting:
The way JS's GC works depends on the implementation: V8 manages the memory quite good, whereas older versions of IE leaked more than a rusty colander.
Setting a variable to null, however does not initialize it. Null
isn't like the null
in other languages (just try typing typeof null
in your console). Try someVar = undefined
instead. Even so, Js does have a reserved delete
keyword which seems to me, to be what you're after.
All in all: if the object is referenced by a variable, or an array: it doesn't really matter: arrays are sparse, so if you use delete someArray[i];
, there really shouldn't be any difference between that or delete someVar;
.
Note: Deleting a variable in the global scope will always return true, but if that variable was explicitly declared (using the var
keyword) or if it's a function, it will not be deleted. Only implied globals can be deleted, so:
var global1 = 'I cannot be deleted';
function global2()
{
global3 = 123;//implied global
}
global4 = function()
{
console.log('implied global function, can be deleted');
};
delete global1;//returns true
console.log(global1);//I cannot be deleted
delete global2;
global2();//shouldn't create global3, because it was deleted but:
console.log(global3);//123
delete global3;//true
console.log(global3);//undefined, was deleted
delete global4;//true
global4();//error <--- was deleted = reference error
However: in both cases, dereferencing any object that is referenced, still, in a closure somewhere or by another variable somewhere in your script will not GC it.
In General, the JS GC's just flag and swipe the memory in cycles: when all references to any given object go out of scope, they are flagged and the memory is deallocated. You don't have any real control over this process, so you can't really optimize your code for this.
Even so, if you still want to go ahead, and use the timeline and profiler tools in FF or Chrome, then be my guest. But do keep in mind: you're optimizing for a particular JS engine when you're doing that: V8-optimized code might generate cleaner memory in Chrome and Safari, but for all you know, IE might make a complete hash of things, or some of the tricks you've used cause FF to slow down, more than a fairly large array would have done in the first place.
I think I'm going to stop this rant here... Bottom line: You can't optimize something over which you have no real control.
Update:
Since I've just read your comment about the mem-leaks you're currently having: I don't think that setting a variable to null
or undefined
or even delete
-ing it is going to do you much good. A far better way to ensure that (modern) GC's can do their job is clever use of closures: whenever a function returns, the variables that were declared in its scope are marked for GC. if those variables are returned, or the return value of that function is either an object or another function, that reference those variables, they won't be GC'ed, but those variables that are no longer referenced are deallocated.
Global variables never go out of scope and are, therefore, never GC'ed. That's why Globals are evil. If you really have a lot of JS code to optimize: start putting things in IIFE's, that can make a huge difference, with very little effort.