Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs for vs map and filter
(version: 0)
looping multiple times or just once over an array to map new values and filter the ouput
Comparing performance of:
forEach vs reduce vs for vs map and filter
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 12345; i++) { arr[i] = i; } function someFn(i) { return (i % 5); } var sumForEach = 0, sumReduce = 0, sumMap = 0, sumFilter = 0, sumFor = 0;
Tests:
forEach
let newArr = [] arr.forEach(item => { if(item) newArr.push(item) });
reduce
let newArr = arr.reduce((lastValue, item) => { if(item) lastValue.push(item); return lastValue }, []);
for
let newArr = [] for (let j of arr) { if(j) newArr.push(j); }
map and filter
let newArr = arr.map(a => a ? a : false).filter(a => a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
reduce
for
map and filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Related benchmarks:
for vs foreach vs map vs (map, filter reduce)
sum calculation: forEach vs for vs forof vs reduce 2
forEach vs reduce vs map vs filter vs for tiny
forEach vs reduce vs map vs filter vs for (slightly optimized for, fixed fn)
Comments
Confirm delete:
Do you really want to delete benchmark?