Last active
January 1, 2016 14:19
-
-
Save NV/8157431 to your computer and use it in GitHub Desktop.
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
<script> | |
function run() { | |
function clicked(e) { | |
console.log(e); | |
} | |
for (var i = 0; i < 1000; i++) { | |
var span = document.createElement('span'); | |
span.addEventListener('click', clicked, false); | |
document.body.appendChild(span); | |
} | |
setTimeout(function() { | |
var spans = document.querySelectorAll('span'); | |
for (var i = 0; i < spans.length; i++) { | |
var span = spans[i]; | |
span.parentNode.removeChild(span); | |
} | |
}, 100); | |
} | |
</script> | |
<button onclick="run()">Run (memory leak)</button> |
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
<script> | |
function run() { | |
function clicked(e) { | |
console.log(e); | |
} | |
for (var i = 0; i < 1000; i++) { | |
var span = document.createElement('span'); | |
span.addEventListener('click', clicked, false); | |
document.body.appendChild(span); | |
} | |
setTimeout(function() { | |
var spans = document.querySelectorAll('span'); | |
for (var i = 0; i < spans.length; i++) { | |
var span = spans[i]; | |
span.removeEventListener('click', clicked, false); // <-- Fix memory leak | |
span.parentNode.removeChild(span); | |
} | |
}, 100); | |
} | |
</script> | |
<button onclick="run()">Run (no memory leak)</button> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment