properties vs arrays
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
function Test(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
};
Test.prototype.doWork = function() {
this.a++;
var r = this.a + this.b*this.c;
this.c = this.c+this.b;
return r;
};
var literalFuns = {
doWork: function() {
this.a++;
var r = this.a + this.b*this.c;
this.c = this.c+this.b;
return r;
}
};
function newLiteral(a, b, c, funs) {
return { a: a, b: b, c: c,
doWork: funs.doWork
};
};
var arrayFuns = [
function( arr ) {
arr[0]++;
var r = arr[0] + arr[1]*arr[2];
arr[2] = arr[2] + arr[1];
return r;
}
];
function newArray( a, b, c, funs ) {
return [
a,
b,
c,
funs[0]
];
}
var obj = new Test(1, 2, 3);
var lit = newLiteral( 1, 2, 3, literalFuns );
var arr = newArray( 1, 2, 3, arrayFuns );
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
properties obj |
|
pending… |
proeprties array |
|
pending… |
properties literals |
|
pending… |
doWork obj |
|
pending… |
doWork literal |
|
pending… |
doWork Array |
|
pending… |
new Object |
|
pending… |
new Literal |
|
pending… |
new Array |
|
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
- Revision 2: published
- Revision 3: published by Yorick
- Revision 4: published
0 comments