filter vs forEach + push (v3)

Revision 3 of this benchmark created on


Setup

const arr = new Array(100000).fill(0).map(e => Math.random())

Test runner

Ready to run.

Testing in
TestOps/sec
filter
const arr1 = arr.filter(e => e > 0.5);
ready
forEach + push
const arr2 = [];

arr.forEach(e => {
	if (e > 0.5) {
		arr2.push(e);
	}
});
ready
for .. of + push
const arr2 = [];

for (const e of arr) {
	if (e > 0.5) {
		arr2.push(e);
	}
}
ready
for + push
const arr2 = [];

for (let i = 0; i < arr.length; i++) {
	if (arr[i] > 0.5) {
		arr2.push(arr[i]);
	}
}
ready

Revisions

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