DEV Community

Discussion on: filter, map and reduce in JS. When and Where to use??

Collapse
lukeshiru profile image
LUKESHIRU

I mean, I tried with 40.000 elements and is still not a huge gap. Browsers nowadays have lots of optimizations for this kind of operations. And idk about you, but I still believe that array.filter(olderThan25).map(getName) is more readable than array.reduce((acc, user) => olderThan25(user) ? acc.concat(getName(user)) : acc) X_X

Thread Thread
uuykay profile image
William Kuang

Performing map, then filter is 2 iterations of the list. Despite some small readability gains, the performance loss is just too great.

Thread Thread
lukeshiru profile image
LUKESHIRU

"performance loss is too great" ... have you actually ran the code above? Browsers optimize those kind of chained operations. The performance difference might br noticeable maybe with, idk, 100k items, but if you have to filter/map that amount of items, your problem is elsewhere.....