Performance of Array vs. Object

JavaScript performance comparison

Revision 78 of this test case created by bluefireuoop

Info

After seeing http://jsperf.com/javascript-associative-vs-non-associative-arrays, I thought the test could be improved.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var i,
        obj = {}, arr1 = new Uint32Array(100000), arr2 = [], arr3 = [];
   
    for(i = 0; i < 100000; i += 1) {
        var p1 = i, p2 = i*i, p3 = Math.sin(i);
   
        obj[i] = {p1:p1, p2:p2, p3:p3};
        arr1[i] = p1;
        arr2[i] = p2;
        arr3[i] = p3;
    }
   
};
</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
Array Performance
var sum = 0;
for (var x=0; x<100000; ++x) {
    sum += arr1[x] + arr2[x] + arr3[x];
}
pending…
Obj perf
var sum = 0;
for (var x=0; x<100000; ++x) {
    var o = obj[x];
    sum += o.p1 + o.p2 + o.p3;
}
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