Array vs hash Performance (v163)

Revision 163 of this benchmark created by Sebastian on


Description

Manual array lookups vs. holey arrays.

Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
Manual Array Lookup
for (var i = 0; i < TEST.array.length; i++) {
  if (TEST.array[i].id == TEST.idToFind) result = TEST.array[i];
}
ready
Holey Array by Index
result = TEST.arrayByIds[TEST.idToFind];
ready
Object by Key
result = TEST.hash[TEST.idToFind];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.