prototype vs Object.create perf
JavaScript performance comparison
Info
Testing the efficiency of different object type creation and call methods
Preparation code
<script>
/*
* "Traditional" Javascript Object
*/
var ObjA = function() {
this.foo = 0;
this._baz = 0;
}
ObjA.prototype.bar = function(value) {
return Math.sqrt(value);
}
ObjA.prototype.setBaz = function(value) {
this._baz = value;
}
ObjA.prototype.getBaz = function() {
return this._baz;
}
var instanceA = new ObjA();
/*
* "ECMA 5-style" Javascript Object
*/
var ObjB = Object.create(Object, {
foo: {
value: 0
},
bar: {
value: function(value) {
return Math.sqrt(value);
}
},
_baz: {
value: 0
},
baz: {
set: function(value) {
this._baz = value;
},
get: function() {
return this._baz;
}
}
});
var instanceB = Object.create(ObjB);
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
new ObjA(); |
|
pending… |
Object.create(ObjB) |
|
pending… |
ObjA member read/write |
|
pending… |
ObjB member read/write |
|
pending… |
ObjA function call |
|
pending… |
ObjB function call |
|
pending… |
ObjA "property" get/set |
|
pending… |
ObjB property get/set |
|
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 Brandon Jones
- Revision 2: published
0 comments