Array Performance

JavaScript performance comparison

Revision 197 of this test case created by

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    var a1 = [{id: 938, name: 'name1'}, {id: 9381, name: 'name1'}, {id: 9382, name: 'name1'}, {id: 9383, name: 'name1'}, {id: 9384, name: 'name1'}, {id: 9385, name: 'name1'}, {id: 9386, name: 'name1'}, {id: 9387, name: 'name1'}, {id: 9388, name: 'name1'}, {id: 9389, name: 'name1'}, {id: 9390, name: 'name1'}, {id: 9391, name: 'name1'}, {id: 32994, name: 'name1'}];

    var a2 = [];
    a2[938] = {id: 938, name: 'name1'};
    a2[9381] = {id: 9381, name: 'name1'};
    a2[9382] = {id: 9382, name: 'name1'};
    a2[9383] = {id: 9383, name: 'name1'};
    a2[9384] = {id: 9384, name: 'name1'};
    a2[9385] = {id: 9385, name: 'name1'};
    a2[9386] = {id: 9386, name: 'name1'};
    a2[9387] = {id: 9387, name: 'name1'};
    a2[9388] = {id: 9388, name: 'name1'};
    a2[9389] = {id: 9389, name: 'name1'};
    a2[9390] = {id: 9390, name: 'name1'};
    a2[9391] = {id: 9391, name: 'name1'};
    a2[32994] = {id: 32994, name: 'name1'};

    var o = {};
    o['938'] = {id: 938, name: 'name1'};
    o['9381'] = {id: 9381, name: 'name1'};
    o['9382'] = {id: 9382, name: 'name1'};
    o['9383'] = {id: 9383, name: 'name1'};
    o['9384'] = {id: 9384, name: 'name1'};
    o['9385'] = {id: 9385, name: 'name1'};
    o['9386'] = {id: 9386, name: 'name1'};
    o['9387'] = {id: 9387, name: 'name1'};
    o['9388'] = {id: 9388, name: 'name1'};
    o['9389'] = {id: 9389, name: 'name1'};
    o['9390'] = {id: 9390, name: 'name1'};
    o['9391'] = {id: 9391, name: 'name1'};
    o['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 = 9387;
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id === id) {
    result = a1[i];
    break
  }
}
pending…
Holey Array by Index
var id = 9387;
for (var i = 0, length = a1.length; i < length; i++) {
  if (a1[i].id === id) {
    result = a1[i];
    break
  }
}
pending…
Object by Key
var id = '9387';
var result = o[id];
pending…
Manual array lookup, using filter
var id = 9387;
var i = a1.length;
while(--i > -1){
  if (a1[i].id === id) {
    result = a1[i];
    break
  }
}
pending…
Manual array lookup, using filter, dot-notation
result = a1.filter(function(elm) {
  return elm.id == 9387;
})[0];
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