Crazy and passionate Front-end Developer. In love with the details of app-dev. Always eager to bring ❤️ to the world in the shape of software and knowledge 😺
Location
Stuttgart, Germany
Education
14 years of autonomous learning of software architecture for web and smart home development
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.
min:
Math.min(...numbers)
max:
Math.max(...numbers)
Thanks for reminding this tip @martinmuzatko .
Please what tool is used to make this code snippet embedment on the pages?
These are just code test only. Use 3 backticks before and after your code. Check the preview.
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