Searching

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Linear search
function linearSearch(arr, el) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === el) {
            return i;
        }
    }

    return -1;
}
ready
Binary search
function binarySearch(arr, el) {
    if (arr.length <= 1) {
        return arr;
    }

    arr.sort();

    let left = 0;
    let right = arr.length - 1;

    while (left <= right) {
        const middle = Math.floor((left + right) / 2);

        if (arr[middle] === el) {
            return middle;
        }

        if (arr[middle] < el) {
            left = middle + 1;
        } else {
            right = middle - 1;
        }
    }

    return -1;
}
ready

Revisions

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