Performance of Array vs. Object

JavaScript performance comparison

Revision 16 of this test case created

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 arr = [],
        obj_arr = [],
        obj = {},
        obj_obj = {},
        i,
        times = 1000;
   
    for(i = 0; i < times; i+=1) {
        arr.push(i);
        obj[i] = i;
        obj_arr[i] = {v: 1};
        obj_obj[i] = {v: 1};
    }
   
};
</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
for (var x=0; x<arr.length; x+=1) {
    arr[x]
}
pending…
Object Performance
for (var x in obj) {
    obj[x]
}
pending…
Object Performance using known keys
for (var x=0; x<times; x+=1) {
    obj[x]
}
pending…
Array of objects Performance
for (var x=0; x<obj_arr.length; x+=1) {
    obj_arr[x]
}
pending…
Object of objects Perfomance
for (var x=0; x<times; x+=1) {
    obj_obj[x]
}
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