DEV Community

How Array.reduce() could replace other array methods

Oussama Bouyahia on January 31, 2024

JavaScript developers use array methods on a daily basis, and it has become imperative to incorporate these methods into the code instead of relyin...
Collapse
 
ohaddahan profile image
ohaddahan

Worth mentioning that using reduce to replace these methods will probably result in poor performance.

In the case of map, pushing on an an empty array is slow and can lead to multiple memory allocations. I assume under the hood map allocates an array of the same size on one go and assign into each element.

Also your every doesn’t early return on the first false. Which can lead to many unneeded iterations.

Collapse
 
oussamabouyahia profile image
Oussama Bouyahia • Edited

that is what I mentioned in the conclusion knowing the mother of methods doesn't mean is the cleanest code in terms of performance