24. LeakyDomNodes
1. var elements = {
2. button: document.getElementById('button'),
3. image: document.getElementById('image'),
4. text: document.getElementById('text')
5. };
6.
7. function doStuff() {
8. image.src = 'http://some.url/image';
9. button.click();
10. console.log(text.innerHTML);
11. // Much more logic
12. }
13.
14. function removeButton() {
15. // The button is a direct child of body.
16. document.body.removeChild(document.getElementById('button'));
17.
18. // At this point, we still have a reference to #button in the global
19. // elements dictionary. In other words, the button element is still in
20. // memory and cannot be collected by the GC.
21. }
28. LeakyEvents-Circularreference
1. var element = document.getElementById('button');
2.
3. function onClick(event) {
4. element.innerHtml = 'text';
5. }
6.
7. element.addEventListener('click', onClick);
8. // Do stuff
9. element.removeEventListener('click', onClick);
10. element.parentNode.removeChild(element);
11. // Now when element goes out of scope,
12. // both element and onClick will be collected even in old browsers that don't
13. // handle cycles well.
30. LeakyClosures
1. var run = function () {
2. var str = new Array(1000000).join('*');
3. var doSomethingWithStr = function () {
4. if (str === 'something')
5. console.log("str was something");
6. };
7. doSomethingWithStr();
8. var logIt = function () {
9. console.log('interval');
10. }
11. setInterval(logIt, 100);
12. };
13. setInterval(run, 1000);