binary search

Benchmark created on


Preparation HTML

<script>
  var myList = [1, 3, 6, 10, 14, 15, 18, 20, 25, 33, 36, 40, 43, 44, 48, 50];
  
  
  function binary_search_iterative2(arr, ele) {
      var beginning = 0, end = arr.length,
          target;
      while (true) {
          target = ((beginning + end) >> 1);
          if ((target === end || target === beginning) && arr[target] !== ele) {
              return -1;
          }
          if (arr[target] > ele) {
              end = target;
          } else if (arr[target] < ele) {
              beginning = target;
          } else {
              return target;
          }
      }
  }
  
  
  
  function binary_search_iterative(arr, ele) {
      var beginning = 0, end = arr.length,
          target;
      while (true) {
          target = Math.floor((beginning + end) / 2);
          if ((target === end || target === beginning) && arr[target] !== ele) {
              return -1;
          }
          if (arr[target] > ele) {
              end = target;
          } else if (arr[target] < ele) {
              beginning = target;
          } else {
              return target;
          }
      }
  }
  
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
new
binary_search_iterative2(myList, 6);
binary_search_iterative2(myList, 15);
binary_search_iterative2(myList, 33);
binary_search_iterative2(myList, 50);
binary_search_iterative2(myList, 1);
ready
original
binary_search_iterative(myList, 6);
binary_search_iterative(myList, 15);
binary_search_iterative(myList, 33);
binary_search_iterative(myList, 50);
binary_search_iterative(myList, 1);
ready

Revisions

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