test

Benchmark created on


Setup

const arr = new Array(1000000);
for (let i = 0; i < arr.lenght; i++) {
	arr[i] = i + 1;
}

Test runner

Ready to run.

Testing in
TestOps/sec
linear
function searchElement(arr, el) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == el) {
      return el;
    }
  }
  return "This number is absent";
}
const el = Math.floor(Math.random() * arr.length)
searchElement(arr, 1000000);
ready
binary
function binarySearch(arr, el) {
  let left = -1;
  let right = arr.length;

  while (right - left > 1) {
    const mid = Math.floor((left + right) / 2);
    // если мы попали на этот элемент приделении массива, то можем вернуть его
    if (arr[mid] === el) {
      return mid;
    }

    if (arr[mid] > el) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return -1;
}
const el = Math.floor(Math.random() * arr.length)
binarySearch(arr, 1000000);
ready

Revisions

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