Skip to content

Instantly share code, notes, and snippets.

@jfirebaugh
Created October 26, 2012 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfirebaugh/3961693 to your computer and use it in GitHub Desktop.
Save jfirebaugh/3961693 to your computer and use it in GitHub Desktop.
<script>
var Test = function () {};
Test.prototype.a = function () {};
Test.prototype.b = function () {};
Test.prototype.c = function () {};
Test.prototype.d = function () {};
Test.prototype.e = function () {};
Test.prototype.f = function () {};
Test.prototype.g = function () {};
Test.prototype.h = function () {};
Test.prototype.i = function () {};
Test.prototype.j = function () {};
Test.prototype.k = function () {};
window.ary = [];
for (var i = 0; i < 10000; ++i) {
ary.push(new Test());
}
</script>
<script>
var Test = function () {
var test = {};
test.a = function () {};
test.b = function () {};
test.c = function () {};
test.d = function () {};
test.e = function () {};
test.f = function () {};
test.g = function () {};
test.h = function () {};
test.i = function () {};
test.j = function () {};
test.k = function () {};
return test;
};
window.ary = [];
for (var i = 0; i < 10000; ++i) {
ary.push(Test());
}
</script>
@jfirebaugh
Copy link
Author

These benchmarks compare the memory usage for classical vs. module pattern objects.

In my tests (Chrome 22.0.1229.94), classical.html generates a 1011 KB heap, while module.html generates a 5.3 MB heap. Most (76%) of the module heap is closures -- one for each function for each instance.

Conclusion: beware using the module pattern for heavily allocated classes.

@jfirebaugh
Copy link
Author

Real-world example from @domenic: jsdom/jsdom#509, fixed by chad3814/CSSStyleDeclaration@33d2ba3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment