Array vs hash Performance

JavaScript performance comparison

Revision 163 of this test case created by Sebastian

Preparation code

<script>
window.TEST = {};
TEST.array = [];
TEST.arrayByIds = [];
TEST.hash = {};
TEST.idToFind = 3290;

(function() {

  var min = 2990;
  var max = 3990;
  var counter = 0;

    for(var i = min; i < max; i++) {
      var record = {id: i, name: 'foo'};
      TEST.array[counter] = record;
      TEST.arrayByIds[i] = record;
      TEST.hash[i] = record;
      counter++;
    }

  console.log(TEST.array)

}())
</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 < TEST.array.length; i++) {
  if (TEST.array[i].id == TEST.idToFind) result = TEST.array[i];
}
pending…
Holey Array by Index
result = TEST.arrayByIds[TEST.idToFind];
pending…
Object by Key
result = TEST.hash[TEST.idToFind];
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