yet another object vs array
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
function Foo( name, a, b ) {
this.name = name;
this.a = a;
this.b = b;
this.c = a + b;
}
Foo.prototype.sum = function() {
return this.a + this.b + this.c;
}
function newFoo( name, a, b ) {
return [ 0, name, a, b, a+b ];
}
function fooSum( fooArr ) {
return fooArr[1] + fooArr[2] + fooArr[3];
}
function newFooLit( name, a, b ) {
return {
_ : 0,
name: name,
a: a,
b: b,
c: a+b
}
}
function fooSumLit( fooLit ) {
if ( fooLit._ === 0 ) {
return fooLit.a + fooLit.b + fooLit.c;
}
}
var fooObj = new Foo( 'john', 2, 4 );
var fooArr = newFoo( 'john', 2, 4 );
var fooLit = newFooLit( 'john', 2, 4 );
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
new with object |
|
pending… |
new with arr |
|
pending… |
new Lit |
|
pending… |
sum with obj |
|
pending… |
sum with arr |
|
pending… |
sum with lit |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments