Last active
August 29, 2015 14:01
-
-
Save dcki/6e52f4c1d8fe789e0a87 to your computer and use it in GitHub Desktop.
JavaScript Constructor.prototype.method saves memory
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
'use strict'; | |
// 56 MB in Chrome using JavaScript Console > Profiles > Take Heap Snapshot. | |
// 117 MB in Firefox viewing the URL about:memory. | |
function A() { | |
this.b = function() { | |
alert('yo'); | |
}; | |
} | |
var a = []; | |
for (var i = 0; i < 1000000; i++) { | |
a.push(new A()); | |
} | |
// 18 MB in Chrome. | |
// 71 MB in Firefox. | |
function A() {} | |
A.prototype.b = function() { | |
alert('yo'); | |
}; | |
var a = []; | |
for (var i = 0; i < 1000000; i++) { | |
a.push(new A()); | |
} | |
// 6 MB in Chrome. | |
// 26 MB in Firefox. | |
var a = []; | |
for (var i = 0; i < 1000000; i++) { | |
a.push(i); | |
} | |
// 1 MB in Chrome. | |
// Not listed in Firefox. Zero? | |
// Nothing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment