array vs object (v18)

Revision 18 of this benchmark created by Ron Buchanan on


Description

This takes out the checking for the array length during the iteration

Setup

var arr = [],
      i,
      obj = {};
      for(i = 0; i < 100; i = i + 1) {
        var item = {index: i};
        arr.push(item);
        obj[i] = item;
      }

Test runner

Ready to run.

Testing in
TestOps/sec
array
var itemCnt = arr.length;
for (var x=0; x<itemCnt; x++) {
    i = arr[x].index;
}
ready
object
for (var x=0; x<100; x++) {
    i = obj[x].index;
}
ready
object unknown length
var keys = Object.keys(obj);
var itemCnt = keys.length;
for (var x = 0; x < itemCnt; x++){
  i = obj[keys[x]].index;
}
ready
arr fastest
var x = 0,
    itemCnt = arr.length;
for (x; x<itemCnt; x++) {
    i = arr[x].index;
}
ready
Test 5
// more real-world object iteration
for (var x in obj) {
    i = obj[x];
}
ready

Revisions

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