Created
April 10, 2015 16:14
-
-
Save avesus/d40d5b0b584c3c52fb6f to your computer and use it in GitHub Desktop.
Closures memleak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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