DEV Community

loading...

Discussion on: Top 20 JavaScript tips and tricks to increase your Speed and Efficiency

Collapse
tomy profile image
Tomy Smith

Exactly, not sure of the advantage of using reduce over these common functions.

Collapse
andrewbridge profile image
Andrew Bridge

I'd go further and agree with this thread and follow up video that advise against using reduce in nearly every situation. It's often used when a simpler or more readable alternative is available, and leads to harder to read, harder to maintain code. As proven with this example above.

Collapse
liquorburn profile image
liquorburn • Edited

I've made a simple benchmark, it seems that reduce() is faster than Math.max()

dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
techygeeky profile image
Kapil Raghuwanshi🖥 Author

That's awesome @liquorburn . I have added the fastest one only.

Thread Thread
tomy profile image
Tomy Smith • Edited

It's actually the spread operator that's taking the time there. Though admittedly, there aren't many cases you'd have a hardcoded list of values to find the max of, it's worth pointing out.

dev-to-uploads.s3.amazonaws.com/up...

Collapse
techygeeky profile image
Kapil Raghuwanshi🖥 Author

Well, of course above methods seems simpler for finding min/max but one should not underestimate the power of reduce in JavaScript. There are several technique where you can solve problems mostly in one reduce statements only.

Thread Thread
erasmuswill profile image
Wilhelm Erasmus

But in this case a helper method that does the full job exists so why bother with reduce for this use case? I admittedly do not use it enough but just saying