Obj Array vs Assoc Array vs Hash Lookup

Benchmark created by Keith on


Preparation HTML

<script>
  var hash = {},
      objArray = [],
      assocArray = [],
      cnt = 1000;
  while (cnt--) {
   var keyStr = "key" + cnt,
       valueStr = "value" + cnt;
   objArray.push({
    "key": keyStr,
    "value": valueStr
   });
   assocArray.push([keyStr, valueStr]);
   hash[keyStr] = valueStr;
  }
  var searchStr = "key500",
      finalValue = "";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Array Loop
for (var i = 0, j = objArray.length; i < j; i++) {
 var obj = objArray[i];
 if (obj.key == searchStr) {
  finalValue = obj.value;
  break;
 }
}
ready
Hash Lookup
finalValue = hash[searchStr];
ready
Assoc Array Loop
for (var i = 0, j = assocArray.length; i < j; i++) {
 var arr = assocArray[i];
 if (arr[0] == searchStr) {
  finalValue = arr[1];
  break;
 }
}
ready

Revisions

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

  • Revision 1: published by Keith on