find element in obj vs array (v35)

Revision 35 of this benchmark created by Koolpin on


Description

slightly better math

Setup

var obj = {},
      arr = [],
      count = 0;
    for (var i = 0; i < 10000; i++) {
      arr.push('wowow124.23' + i);
      obj['wowow124.23' + i] = 1;
    }
    
    
    function in_array(needle, haystack) {
      var i = haystack.length
      while (i--) {
        if (haystack[i] == needle)
          return true;
      }
      return false;
    }
    
    function include(needle, haystack) {
      return (haystack.indexOf(needle) != -1);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
find in array (123)
if (in_array('wowow124.23123', arr)) {
  count++;
}
ready
find in array (1234)
if (in_array('wowow124.231234', arr)) {
  count++;
}
ready
find in array (9543)
if (in_array('wowow124.239543', arr)) {
  count++;
}
ready
find in obj (123)
if (obj['wowow124.23123']) {
  count++;
}
ready
find in obj (1234)
if (obj['wowow124.231234']) {
  count++;
}
ready
find in obj (9543)
if (obj['wowow124.239543']) {
  count++;
}
ready
indexOf in arr(123)
if (include('wowow124.23123', arr)) {
  count++;
}
ready
indexOf in arr(1234)
if (include('wowow124.231234', arr)) {
  count++;
}
ready
indexOf in arr(9543)
if (include('wowow124.239543', arr)) {
  count++;
}
ready

Revisions

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