Performance of Array vs. Object (v73)

Revision 73 of this benchmark created on


Description

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

Setup

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;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array Performance
var x;
for(i = 0; i < 100; i += 1) {
    x = arr.indexOf(i);
}
ready
Object Performance
var x;
for(i = 0; i < 100; i += 1) {
    x = obj[i];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.