for..of versus filter + map

Benchmark created on


Description

JavaScript for..of loop compared to .filter().map()

Setup

const arr = new Array(1000);

for (let i = 0; i < 1000; i++) {
	const random = Math.random();
	arr[i] = {
		isEven: i % 2 === 0,
		random,
	};
}

Test runner

Ready to run.

Testing in
TestOps/sec
Filter + map
const results = arr
	.filter((item, i) => item.isEven)
	.map((item) => Math.floor(item.random * 100));
ready
for..of
const results = [];

for (const item of arr) {
	if (item.isEven) {
		results.push(Math.floor(item.random * 100));
	}
}
ready

Revisions

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