Array Performance

JavaScript performance comparison

Revision 29 of this test case created by David Bonnet

Info

Manual array lookups vs. holey arrays.

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    A1 = [];
    O1 = {};
    var amount=50000;
    for (var i=0; i < amount; i++) {
      A1.push({id: i, name: 'name1'});
      O1[''+i] = {id: i, name: 'name1'};
    }

    A2 = [];
    A2[20000] = {id: 20000, name: 'name1'};
    A2[30000] = {id: 30000, name: 'name1'};

    A3 = [{id: 20000, name: 'name1'}, {id: 30000, name: 'name1'}];
  };
</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
Manual Array Lookup
var id = 30000, result, a = A1, l = a.length;
for (var i = 0; i < l; i++) {
  if (a[i].id == id) {
    result = a[i];
    break;
  }
}
pending…
Holey Array by Index
var id = 30000, result = A2[id];
pending…
Object by Key String
var id = 30000, result = O1[id.toString()];
pending…
Manual Array Lookup (small)
var id = 30000, result, a = A3, l = a.length;
for (var i = 0; i < l; i++) {
  if (a[i].id == id) {
    result = a[i];
    break;
  }
}
pending…
Object by Key Index
var id = 30000, result = O1[id];
pending…
Array by Index
var id = 30000, result = A1[id];
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