properties vs arrays

JavaScript performance comparison

Revision 2 of this test case created

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.

Testing in unknown unknown
Test Ops/sec
properties obj
obj.a++;
var r = obj.a + obj.b*obj.c;
obj.c = obj.c + obj.b;
pending…
proeprties array
arr[0]++;
var r = arr[0] + arr[1]*arr[2];
arr[2] = arr[2] + arr[1];
pending…
properties literals
lit.a++;
var r = lit.a + lit.b*lit.c;
lit.c = lit.c + lit.b;
pending…
doWork obj
var r = obj.doWork();
pending…
doWork literal
var r = lit.doWork();
pending…
doWork Array
var r = arr[3]( arr );
pending…
new Object
var o = new Test( 5, 6, 7 );
pending…
new Literal
var l = newLiteral( 5, 6, 7, literalFuns );
pending…
new Array
var a = newArray( 5, 6, 7, arrayFuns );
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:

0 comments

Add a comment