Skip to content

Instantly share code, notes, and snippets.

@avesus
Created April 10, 2015 16:14
Show Gist options
  • Save avesus/d40d5b0b584c3c52fb6f to your computer and use it in GitHub Desktop.
Save avesus/d40d5b0b584c3c52fb6f to your computer and use it in GitHub Desktop.
Closures memleak
// Because closure is not A variable, but ALL OF THEM in dict (i.e., str, doSomethingWithStr, logIt).
// Referencing logIt in setInterval(logIt, 100) long lasts not only logIt, but also all another vars -
// for the lexical scope to be consistent.
var run = function () {
var str = new Array(1000000).join('*');
var doSomethingWithStr = function () {
if (str === 'something')
console.log("str was something");
};
doSomethingWithStr();
var logIt = function () {
console.log('interval');
}
setInterval(logIt, 100);
};
setInterval(run, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment