Skip to content

Instantly share code, notes, and snippets.

@dcki
Last active August 29, 2015 14:01
Show Gist options
  • Save dcki/6e52f4c1d8fe789e0a87 to your computer and use it in GitHub Desktop.
Save dcki/6e52f4c1d8fe789e0a87 to your computer and use it in GitHub Desktop.
JavaScript Constructor.prototype.method saves memory
'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