Array vs Object Performance

JavaScript performance comparison

Revision 302 of this test case created by Derek Detweiler

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    var lookup = 0, lookupString = '';
    var i = 3000;
    var id = 0;
    var a1 = [];
    var a2 = [];
    var o = {};
    var m = new Map();

    a1.length = 3000;
    while (i--) {
        id = Math.floor(Math.random() * 100000);
        m.set(id, a1[i] = a2[id] = o[id + ''] = {id: id, name: 'name' + i});
    }

    lookup += a1[1500].id;
    lookupString += lookup;
  };
</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
for (var i = 0; i < a1.length; i++) {
  if (a1[i].id === lookup) {
    result = a1[i];
    break;
  }
}
pending…
Holey Array by Index
var result = a2[lookup];
pending…
Object by Key
var result = o[lookupString];
pending…
Map API
var result = m.get(lookup);
pending…
Object by Key (Number)
var result = o[lookup];
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