Performance of Array vs. Object (v70)

Revision 70 of this benchmark created on


Description

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

Made lengths consistent and fattened element number to 100,000. - KAS

Initialized vars in the setup, added "Object While" loop - bmcgin

Setup

var arr = [],
      i,
      obj = {};
    for (i = 0; i < 100000; i += 1) {
      var o = {
        payload: i
      };
      arr.push(o);
      obj[i] = o;
    }
    
    var sum = 0;
    var keys = Object.keys(obj);
    var len = keys.length;
    var i = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
Array Performance
for (var x = 0; x < 100000; ++x) {
  sum += arr[x].payload
}
ready
Object Performance
for (var x in obj) {
  sum += obj[x].payload;
}
ready
Object Performance using known length
for (var x = 0; x < 100000; ++x) {
  sum += obj[x].payload
}
ready
Using Object.keys()
for (var x = 0; x < keys.length; ++x) {
  sum += obj[keys[x]].payload
}
ready
Array Performance known length
for (var x = 0; x < 100000; ++x) {
  sum += arr[x].payload
}
ready
Object Keys - While
i = 0;
while (i < len) {
  sum += obj[keys[i]].payload
  i++;
}
ready

Revisions

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