find element in obj vs array (v42)

Revision 42 of this benchmark created by benfloyd 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 in_array2(needle, haystack) {
      for(i=haystack.length;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 array1 (123)
if (in_array('wowow124.23123', arr)) {
  count++;
}
ready
find in array1 (1234)
if (in_array('wowow124.231234', arr)) {
  count++;
}
ready
find in array1 (9543)
if (in_array('wowow124.239543', arr)) {
  count++;
}
ready
find in array2 (123)
if (in_array2('wowow124.23123', arr)) {
  count++;
}
ready
find in array2 (1234)
if (in_array2('wowow124.231234', arr)) {
  count++;
}
ready
find in array2 (9543)
if (in_array2('wowow124.239543', arr)) {
  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.