Modern engines will not support unused variables in the external area.
Therefore, it doesnβt matter if you set data = null before returning the inner function, because the inner function does not depend on ("close") data .
If the inner function did depending on the data - perhaps it returns it - then setting data = null is definitely not what you want, because then, well, that would be null, instead of having its original value!
Assuming that the inner function really depends on data , then yes, as long as inner points to something, then the value of data should be saved. But what you say you want! How can you get something available without its availability?
Remember that at some point, the variable that holds the return value of f() will itself go out of scope. At this point, at least until f() is called again, data will be garbage collected.
The general rule is that you donβt need to worry about memory and leak using JavaScript. This is the whole point of GC. The garbage collector does an excellent job of identifying what is needed and what is not needed, and preserving the former and the garbage collecting the latter.
You can consider the following example:
function foo() { var x = 1; return function() { debugger; return 1; }; } function bar() { var x = 1; return function() { debugger; return x; }; } foo()(); bar()();
And look at its execution in the devtools Chrome Variables window. When the debugger stops in the internal function foo , note that x not present as a local variable or as a closure. For all practical purposes, it does not exist.
When the debugger stops in the internal function bar , we see the variable x , because it must be saved in order to be available for return.
It's right? Or is this article out of date?
No, it is not, and yes, it is. The article is four years old, and it's a lifetime in the world of the Internet. I donβt know if jQuery can be leaked, but I would be surprised if that were the case, and if so, there is a fairly simple way to avoid them - do not use jQuery. The leaks mentioned by the author of the article related to DOM loops and event handlers are absent in modern browsers, which I mean IE10 (rather IE9) and higher. I would suggest finding a more relevant link if you really want to understand a memory leak. In fact, I suggest you basically stop worrying about memory leaks. They occur only in very specialized situations. It's hard to find a lot on this subject online these days for this exact reason. Here is one article I found: http://point.davidglasser.net/2013/06/27/surprising-javascript-memory-leak.html .