binary search

JavaScript performance comparison

Test case created by

Preparation code

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/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);
pending…
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);
pending…

Compare results of other browsers

Revisions

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

0 Comments