Find first available number

Benchmark created on


Setup

const numbers = Array.from({ length: 10000 }, (_, i) => i).sort(() => Math.random() > 0.5 ? 1 : -1);

console.log(numbers);

Test runner

Ready to run.

Testing in
TestOps/sec
Simple includes
let index = 0;

while (numbers.includes(index)) {
	index++;
}

console.log(index);
ready
Sort and iterate
const sorted = numbers.toSorted((a, b) => a - b);
let index = 0;

for (const number of sorted) {
	if (index === number) {
		index++;
	} else {
		break;
	}
}

console.log(index);
ready
Sorted includes
const sorted = numbers.toSorted((a, b) => a - b);
let index = 0;

while (numbers.includes(sorted)) {
	index++;
}

console.log(index);
ready

Revisions

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