Array Performance

JavaScript performance comparison

Revision 145 of this test case created by Jonathan Perry

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    var a1 = [{id: 29938, name: 'name1'}, {id: 32994, name: 'name1'}];

    var a2 = [];
    a2[29938] = {id: 29938, name: 'name1'};
    a2[32994] = {id: 32994, name: 'name1'};

    var o1 = { 0: {id: 29938, name: 'name1'}, 1: {id: 32994, name: 'name1'} };

    var o2 = { 29938: {id: 29938, name: 'name1'}, 32994: {id: 32994, name: 'name1'} };

  };
</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
Manual Array Lookup
var id = 29938;
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id = id) result = a1[i];
}
pending…
Holey Array by Index
var id = 29938;
var result = a2[id];
pending…
Object by Key (1)
var id = 29938;
var result = o2[id];
pending…
Manual Array Lookup with variables
var id = 29938;
for (var i = 0, aval = a1.length; i < aval; i++) {
  if (a1[i].id = id) result = a1[i];
}
pending…
Manual Array Lookup with variables and break
var id = 29938;
for (var i = 0, aval = a1.length; i < aval; i++) {
  if (a1[i].id = id) {
    result = a1[i];
    break;
  }
}
pending…
Array by Index
var id = 0;
var result = a1[id];
pending…
Object by Key (2)
var id = 0;
var result = o1[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.

1 Comment

Jonathan Perry (revision owner) commented :

"Holey Array by Index (1)" creates a 32995 length sparse array; variations on the "Manual Array Lookup" test create a 2 length populated array (introducing a "break" statement doubles the performance of the test by halving the iteration; the test is seeking the first item of two, and finds it).