find element in obj vs array (v46)

Revision 46 of this benchmark created by DJ on


Setup

var obj = {},
        arr = [],
        count = 0;
    for (var i = 0; i < 1000; i++) {
      arr.push(i);
      obj[i] = 1;
    }
    
    
    function in_array(needle, haystack) {
      for (var i = 0, maxi = haystack.length; i < maxi; ++i) {
        if (haystack[i] == needle) {
          return true;
        }
      }
      return false;
    }
    
    function in_arrayIdentity(needle, haystack) {
      for (var i = 0, maxi = haystack.length; i < maxi; ++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 (1)
if (in_array(1, arr)) {
  count++;
}
ready
find in array (5)
if (in_array(5, arr)) {
  count++;
}
ready
find in array (9)
if (in_array(9, arr)) {
  count++;
}
ready
find in obj (1)
if (obj[1]) {
  count++;
}
ready
find in obj (5)
if (obj[5]) {
  count++;
}
ready
find in obj (9)
if (obj[9]) {
  count++;
}
ready
indexOf in arr(1)
if (include(1, arr)) {
  count++;
}
ready
indexOf in arr(5)
if (include(5, arr)) {
  count++;
}
ready
indexOf in arr(9)
if (include(9, arr)) {
  count++;
}
ready
find in array Identity (1)
if (in_arrayIdentity(1, arr)) {
  count++;
}
ready
find in array Identity (5)
if (in_arrayIdentity(5, arr)) {
  count++;
}
ready
find in array Identity (9)
if (in_arrayIdentity(5, arr)) {
  count++;
}
ready

Revisions

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