Performance of Array vs. Object

JavaScript performance comparison

Revision 73 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 = [],
        i,
        obj = {};
    for(i = 0; i < 100; i += 1) {
        arr.push(i);
        obj[i] = i;
    }
   
    function getRandomIndex(obj) {
       var idx = Math.floor(Math.random()*9999);
       if(false === obj.hasOwnProperty(idx)) {
          while (false === obj.hasOwnProperty(idx)) {
              idx = Math.floor(Math.random()*9999);
          }
       }
       return idx;
    }
};
</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 x;
for(i = 0; i < 100; i += 1) {
    x = arr.indexOf(i);
}
pending…
Object Performance
var x;
for(i = 0; i < 100; i += 1) {
    x = obj[i];
}
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