Spare array iteration

Benchmark created on


Description

When iterating over a spare array, where not all indexes are consecutive, for..of and tradition for loops are not an option. Hence, we need to use other methods. This tests these other methods' performance.

Setup

const test = [];

for(let i = 0; i < 10000; i += Math.floor(Math.random() * 32)) {
	test[i] = Math.random();
}


function doSomething(i, val) {
	// Do something
}

Test runner

Ready to run.

Testing in
TestOps/sec
Array.prototype.forEach
test.forEach((i, val) => {
	doSomething(i, val);
});
ready
for..in
for(let i in test) {
	doSomething(i, test[i]);
}
ready

Revisions

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