Performance of Array vs. Object (v24)

Revision 24 of this benchmark created by Marcin on


Description

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

I thought it was conspicuous that on Chrome, the Object iteration with known length was faster that a pure array (unlike in FireFox). The problem was the arr.length property was accessed on every iteration (and Firefox was optimizing it).

Setup

var arr = [],
        i, obj = {};
    for (i = 0; i < 10; i += 1) {
      var o = {
        payload: i
      };
      arr.push(o);
      obj[i] = o;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array Performance
var sum = 0;
for (var x = 0; x < 10; ++x) {
  sum += arr[x].payload
}
ready
Object Performance using known length
var sum = 0;
for (var x = 0; x < 10; ++x) {
  sum += obj[x].payload
}
ready

Revisions

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