yet another object vs array

JavaScript performance comparison

Test case created

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.

Testing in unknown unknown
Test Ops/sec
new with object
var f1 = new Foo("john", 2, 4);
pending…
new with arr
var f2 = newFoo( "john", 2, 4 );
pending…
new Lit
var f3 = newFooLit( 'john', 2, 4 );
pending…
sum with obj
var c1 = fooObj.sum();
pending…
sum with arr
var c2 = fooSum( fooArr );
pending…
sum with lit
var c3 = fooSumLit( fooLit );
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment