if vs. abused for

Benchmark created on


Setup

const arr = [];
for (let i = 0; i < 10000; i++) {
	arr.push(Math.random() < 0.5 ? `${performance.now()}` : Math.random());
}

let r;

Test runner

Ready to run.

Testing in
TestOps/sec
if
for (let i = 0; i < arr.length; i++) {
	const a = arr[i];
	if (typeof a === "string") {
		r = a;
	}
}
ready
for
for (let i = 0; i < arr.length; i++) {
	for (const a = arr[i]; a === "string"; ) {
		r = a;
		break;
	}
}
ready

Revisions

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