Performance of Array with holes vs. Object with numeric keys

JavaScript performance comparison

Revision 49 of this test case created by gorhill

Info

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

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 unknown unknown
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. Here’s a list of current revisions for this page:

0 comments

Add a comment