Array filtering and mapping methods

Benchmark created on


Setup

const arr = [];
for(let x=0; x<1; x+=0.001){
	arr.push(x);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Map & Filter
arr.filter(x => x<.5).map(x => x*1000);
ready
Flat Map
arr.flatMap((x)=> x<.5?[x*1000]:[]);
ready
Reduce
arr.reduce((acc, x)=>{
	if(x<.5){
		acc.push(x);
	}
	return acc;
}, []);
ready
For in
const res=[];
for(let x in arr){
	if(x<.5){
		res.push(x);
	}
}
ready
For of
const res=[];
for(const x of arr){
	if(x<.5){
		res.push(x);
	}
}
ready
Normal For
const res=[];
for(let i = 0; i < arr.length; i++){
	if(arr[i]<.5){
		res.push(arr[i]);
	}
}
ready

Revisions

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