Performance of Array with holes vs. Object with numeric keys (v49)

Revision 49 of this benchmark created by gorhill on


Description

There are "holes" in the array, and I cared about only these two tests, so I removed all the others.

Setup

// 37.5% of the values in the array are "holes".
    // There is a point where the number of holes justifies
    // using an Object instead of an Array
    var arr = [],
        i, obj = {};
    for (i = 15; i < 40; i += 1) {
      var o = {
        payload: i
      };
      arr[i] = o;
      obj[i] = o;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Array Performance
var sum = 0;
var x = arr.length;
var v;
while (x--) {
  v = arr[x];
  if ( v ) {
    sum += v;
  }
}
ready
Object Performance using known length
var sum = 0;
var xs = Object.keys(obj);
var i = xs.length;
var v;
while (i--) {
  v = obj[xs[i]];
  if ( v ) {
    sum += v;
  }
}
ready

Revisions

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