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.
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.
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.
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
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Exactly, not sure of the advantage of using reduce over these common functions.
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.
I've made a simple benchmark, it seems that reduce() is faster than Math.max()
dev-to-uploads.s3.amazonaws.com/up...
That's awesome @liquorburn . I have added the fastest one only.
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...
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.
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