Performance of Array vs. Object

JavaScript performance comparison

Revision 2 of this test case created by hyphen

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = [],
      i,
      obj = {};
  for(i = 0; i < 10000; 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 CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array Performance
var idx = getRandomIndex(arr),
    a = arr[idx];

/*arr[idx] = Math.floor(Math.random()*9999);
arr.splice(idx,0);
arr[length] = a;*/
 
pending…
Object Performance
var idx = getRandomIndex(obj),
    a = obj[idx];

/*obj[idx] = Math.floor(Math.random()*9999);
delete obj[idx];
obj[idx] = a;*/
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.

0 Comments