Prototype vs Module pattern performance
JavaScript performance comparison
Info
I have refactored these tests entirely.
Someone wrote the formulation:
for (var i=2000; i=0; i--) {
}
Possibly they did not notice that any statement contained by those curly braces will never execute. Paste the following into your JS console:
for (var i=2000; i=0; i--) { throw "An error"; }
You will never see the error "An error" thrown. "i=0" is falsy false. Presumably their intention was that this line should not execute; in which case, I can't see the point of it. Each test instantiated one object outside the "for" loop, regardless, but I think it's strange to include statements which do nothing.
Assuming that the object literal "objectLiteral" had been defined in the preparation or set-up code, someone had also written the statement:
a.push(new objectLiteral());
Because this statement appeared inside the curly braces of the never-executing "for" loop, a lurking error had never been thrown: "objectLiteral" is not a constructor.
I have refactored the object literal test to include the creation of the object literal; anything else does not seem to be a valid test of object creation. Every other test instantiates an object with "new"; pointing a variable at an object which has already been created outside of the test isn't the same thing, which will account for that test outperforming all of the other tests.
o = new ConstructedObject();
And
o = { };
Are as equivalent as can be, assuming only that the constructor "ConstructedObject" is defined outside the test, in the preparation code or set-up. If "objectLiteral" had been defined outside the test, similarly, then this following statement would not be equivalent to either of those above:
o = objectLiteral;
It simply assigns the existing object "objectLiteral" to variable "o".
Some tests use function declarations while others use function expressions. Again, these are not entirely equivalent but for the most part I haven't massaged those inequivalences away.
I have refactored all the methods to do something, and removed all references to the console within those methods, because that seems to be something too much. Since the never-executing "for" loop was duplicated within the test, the statements the duplicated loop contained were not executing, either: I have removed everything that they contained, and deleted the array "a" that was not being populated.
Preparation code
<script type="text/javascript">
function TraditionalPrototypeClass() { }
TraditionalPrototypeClass.prototype.foo = function () {
return 'foo';
};
TraditionalPrototypeClass.prototype.bar = function () {
return 'bar';
};
function TraditionalClass() {
this.foo = function () {
return 'foo';
};
this.bar = function () {
return 'bar';
};
}
var ModulePatternTraditionalPrototypeClass = (function () {
function ModulePatternTraditionalPrototypeClass() { }
ModulePatternTraditionalPrototypeClass.prototype.foo = function () {
return 'foo';
};
ModulePatternTraditionalPrototypeClass.prototype.bar = function () {
return 'bar';
}
return ModulePatternTraditionalPrototypeClass;
}());
var ModulePatternTraditionalClass = (function () {
function foo() {
return 'foo';
}
function bar() {
return 'bar';
}
return function () {
this.foo = foo;
this.bar = bar;
};
}());
function FakeClass() {
function __constructor() {
this.init();
}
__constructor.prototype.init = function () { };
__constructor.prototype.foo = function () {
return 'foo';
};
__constructor.prototype.bar = function () {
return 'bar';
};
return new __constructor();
}
function FakeClassWithoutInit() {
function __constructor() { }
__constructor.prototype.foo = function(){
return 'foo';
};
__constructor.prototype.bar = function(){
return 'bar';
};
return new __constructor();
}
</script>
<script>
Benchmark.prototype.setup = function() {
var o;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Traditional Prototypal Class |
|
pending… |
Traditional Class |
|
pending… |
Object Literal |
|
pending… |
Fake Class |
|
pending… |
Fake Class without init |
|
pending… |
Module Pattern Traditional Prototype Class |
|
pending… |
Module Pattern Traditional Class |
|
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
0 comments