Finding Two Numbers That Sum to Target

Benchmark created on


Setup

let arr = [2, 7, 5, 4, 9, 12];
let target = 9;

function findDoubleSum(arr, target) {
    let numMap = new Map();
    for (let i = 0; i < arr.length; i++) {
        let compData = target - arr[i];
        if (numMap.has(compData)) {
            return [compData, arr[i]];
        }
        numMap.set(arr[i], i);
    }
    return null;
}

Teardown


Test runner

Ready to run.

Testing in
TestOps/sec
Multiple Executions
findDoubleSum(arr, target);
ready
фцыа
for (let i = 0; i < 1000; i++) {
    findDoubleSum(arr, target);
}
ready

Revisions

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