Prototype vs Module pattern performance
JavaScript performance comparison
Info
Removed iterations in tests, because jsperf already does that for us. And renamed variables to be more meaningful to us humans. And other cosmetic changes.
The most important thing to remember is to use the right tool for the job. All these tests do is reference an object with a complex memory allocation. When you don't need something fancy, you're better off using a regular old object
Preparation code
<script>
Benchmark.prototype.setup = function() {
function TraditionalPrototypeClass() {
}
TraditionalPrototypeClass.prototype.foo = function() {
};
TraditionalPrototypeClass.prototype.bar = function() {
};
function ModulePatternClass() {
this.foo = function() {
};
this.bar = function() {
};
}
var ModuleCachePatternClass = (function () {
function foo() {
}
function bar() {
}
return function () {
this.foo = foo;
this.bar = bar;
};
}());
var standardObject = {
foo: function(){
},
bar: function(){
}
};
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Prototypal |
|
pending… |
Module pattern |
|
pending… |
Module pattern with cached functions |
|
pending… |
Use the right tool for the job |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by WTK
- Revision 2: published
- Revision 3: published
- Revision 4: published
- Revision 5: published by test
- Revision 6: published
- Revision 7: published
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 13: published
- Revision 14: published by Paul Comanici (darkyndy)
- Revision 17: published
- Revision 18: published
- Revision 19: published by Witali
- Revision 20: published
- Revision 21: published by Stuart
- Revision 23: published
- Revision 24: published by Carl
- Revision 25: published
- Revision 26: published by Andrew Petersen
- Revision 28: published by Chris
- Revision 29: published
- Revision 30: published by sim
- Revision 31: published
- Revision 32: published
- Revision 33: published
- Revision 34: published
- Revision 36: published by lenzhang
- Revision 37: published by lenzhang
- Revision 39: published by Grant Kiely
- Revision 40: published by Grant Kiely
- Revision 41: published by Grant Kiely
- Revision 42: published by Grant Kiely
- Revision 43: published by Grant Kiely
- Revision 44: published by Grant Kiely
- Revision 46: published by Aaron
- Revision 54: published by Aaron
- Revision 55: published by Montana Harkin
- Revision 56: published
- Revision 57: published
- Revision 61: published
- Revision 62: published
- Revision 63: published
- Revision 64: published by Jonathan Perry
- Revision 65: published by Jonathan Perry
- Revision 67: published by ainthek
- Revision 68: published
- Revision 71: published
- Revision 72: published
- Revision 73: published
- Revision 74: published
- Revision 75: published
- Revision 76: published
- Revision 77: published
- Revision 78: published
- Revision 79: published
- Revision 80: published by Cristian
- Revision 81: published
- Revision 82: published
- Revision 83: published
- Revision 84: published
- Revision 85: published
- Revision 86: published
- Revision 88: published
- Revision 91: published
- Revision 92: published
- Revision 94: published
- Revision 96: published
- Revision 97: published
- Revision 98: published
- Revision 99: published by David
- Revision 100: published by Ryan
- Revision 101: published
- Revision 102: published
- Revision 103: published
- Revision 104: published
1 comment
Your "right tool" test case isn't actually creating a new object. See my revision.