Array vs Object Performance

JavaScript performance comparison

Revision 206 of this test case created by

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    var a = [];
    a[5650] = {id: 5650, name: 'name1'};
    a[13489] = {id: 13489, name: 'name2'};
    a[29938] = {id: 29938, name: 'name3'};
    a[32994] = {id: 32994, name: 'name4'};
    a[47865] = {id: 47865, name: 'name5'};

    var o = {};
    o['5650'] = {id: 5650, name: 'name1'};
    o['13489'] = {id: 13489, name: 'name2'};
    o['29938'] = {id: 29938, name: 'name3'};
    o['32994'] = {id: 32994, name: 'name4'};
    o['47865'] = {id: 47865, name: 'name5'};
  };
</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 < a.length; i++) {
  if (a[i].id = id) result = a[i];
}
pending…
Holey Array by Index
var id = 29938;
var result = a[id];
pending…
Object by Key
var id = '29938';
var result = o[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.

0 Comments