linear or binary search

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
binarySearch
const binarySearch = (arr, el) => {
  let left = -1;
  let right = arr.length;

  while (right - left > 1) {
    const mid = Math.floor(right + left / 2);
    if (arr[mid] === el) {
      return mid;
    }
    if (arr[mid] > el) {
      right = mid;
    } else {
      left = mid;
    }
  }
  retun - 1;
};
ready
linearSearch
const linearSearch = (arr, el) => {
  // Проходим по каждому элементу массива
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === el) {
      return i; 
    }
  }
  return -1; 
};
ready

Revisions

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