indexOf vs hash table lookup (v2)

Revision 2 of this benchmark created by Nathan Sweet on


Preparation HTML

<script>
  var indexedArray, regArray;
  regArray = [{
    id: "lkjasdlfkj",
    body: "hasdfkasdf"
  }, {
    id: "81734098ifh",
    body: "asdfjklasdjf"
  }, {
    id: "08huouh00ds",
    body: "10384hudhsfjkh"
  }, {
    id: "asdf0834jn",
    body: "asdf89034jkiu"
  }, {
    id: "asd0832ksajdfij",
    body: "asdlkfj8ioj98"
  }];
  indexedArray = {
    lkjasdlfkj: {
      body: "hasdfkasdf"
    },
    "81734098ifh": {
      body: "asdfjklasdjf"
    },
    "08huouh00ds": {
      body: "10384hudhsfjkh"
    },
    "asdf0834jn": {
      body: "asdf89034jkiu"
    },
    "asd0832ksajdfij": {
      body: "asdlkfj8ioj98"
    }
  };
  
  regArray2 = ['hasdfkasdf','1234123','asdfasdf','asdfasdf2','qwerqwer','tyuityu','zxcvzxcv','vbnvbnm','768967896789'];
  indexedArray2 = {
  'hasdfkasdf':true,
  '1234123':true,
  'asdfasdf':true,
  'asdfasdf2':true,
  'qwerqwer':true,
  'tyuityu':true,
  'zxcvzxcv':true,
  'vbnvbnm':true,
  '768967896789':true
  };
  var obj;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
array indexOf lookup
regArray[regArray.indexOf({
  id: "asdf0834jn",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "81734098ifh",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "lkjasdlfkj",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "asd0832ksajdfij",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "81734098ifh",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "lkjasdlfkj",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "asd0832ksajdfij",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "08huouh00ds",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "asdf0834jn",
  body: "hasdfkasdf"
})];
regArray[regArray.indexOf({
  id: "08huouh00ds",
  body: "hasdfkasdf"
})];
ready
hash table lookup
indexedArray["08huouh00ds"];
indexedArray["08huouh00ds"];
indexedArray["asdf0834jn"];
indexedArray["lkjasdlfkj"];
indexedArray["81734098ifh"];
indexedArray["asdf0834jn"];
indexedArray["asd0832ksajdfij"];
indexedArray["81734098ifh"];
indexedArray["lkjasdlfkj"];
indexedArray["asd0832ksajdfij"];
ready
array indexOf lookup2
regArray2.indexOf('vbnvbnm') > -1;
 
ready
hash look up 2
obj = JSON.parse('{"'+regArray2.join('":true,"')+'":true}');
obj['vbnvbnm'] === true;
ready

Revisions

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

  • Revision 1: published by meelash on
  • Revision 2: published by Nathan Sweet on