inArray

Benchmark created by frio80 on


Description

Checking if an value is present in an array. Of course, the value we are searching for has and the position in the array has costs.

Preparation HTML

<script>
  function inArray1(needle, haystack) {
   var length = haystack.length;
   for (var i = 0; i < length; i++) {
    if (haystack[i] == needle) return true;
   }
   return false;
  }
  
  function inArray2(needle, haystack) {
   return !!~haystack.join("").indexOf(needle);
  }
  
  function inArray3(needle, haystack) {
   return !haystack.join("").indexOf(needle) == -1
  }
  
  function inArray4(needle, haystack) {
   return !haystack.indexOf(needle) == -1
  }
  
  var arr = [1, 2, 3, 4, 5, 6, 7],
      num = 5;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
inArray1
inArray1(5, arr);
ready
inArray2
inArray2(5, arr);
ready
inArray3
inArray3(5, arr);
ready
inArray4
inArray4(5, arr);
ready

Revisions

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

  • Revision 1: published by frio80 on
  • Revision 2: published by Jens Roland on