Performance of Array with holes vs. Object with numeric keys

JavaScript performance comparison

Revision 49 of this test case created by gorhill

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  // 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;
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array Performance
var sum = 0;
var x = arr.length;
var v;
while (x--) {
  v = arr[x];
  if ( v ) {
    sum += v;
  }
}
pending…
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;
  }
}
pending…

Compare results of other browsers

Revisions

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

0 Comments