Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get max from an array of numbers (Math.max vs. iteration) V3
(version: 0)
Comparing performance of:
Math.max vs Reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [...Array(500)].map(() => Math.floor(Math.random() * 100000));
Tests:
Math.max
return Math.max(...arr);
Reduce
return arr.reduce((maxi, curr) => maxi < curr ? curr : maxi, -1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.max
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 138 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.max
935038.9 Ops/sec
Reduce
1453377.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The MeasureThat.net benchmark tests the performance of two approaches to find the maximum value in an array of numbers: using `Math.max` and using the `reduce()` method. **Test Case 1: Math.max** This test case uses the built-in `Math.max` function, which returns the largest of zero or more numbers. The script preparation code generates a random array of 500 integers between 0 and 100000. This is done to ensure that the benchmark results are representative of real-world scenarios. **Pros of using Math.max:** 1. **Concise**: The `Math.max` function is concise and easy to read. 2. **Well-established**: It's a well-established and widely supported function in JavaScript. **Cons of using Math.max:** 1. **Single-purpose**: It only finds the maximum value, which might not be useful in all scenarios. 2. **Limited control**: There's limited control over how the function handles edge cases or NaN values. **Test Case 2: Reduce** This test case uses the `reduce()` method to find the maximum value in the array. The reduce function applies a specified function against an accumulator and each element of the array (from left to right) to reduce it to a single output value. **Pros of using Reduce:** 1. **Flexibility**: The reduce function can be used for various purposes, such as finding the minimum or sum of values. 2. **Control**: It provides more control over how edge cases and NaN values are handled. **Cons of using Reduce:** 1. **Less concise**: The `reduce()` method requires creating an initial accumulator value and writing a function to accumulate it. 2. **More complex**: It might be less readable for those unfamiliar with the reduce function. **Library Used:** The `Math.max` function does not rely on any external libraries, as it's a built-in JavaScript function. **Special JS Feature/Syntax:** This benchmark uses no special features or syntax beyond standard JavaScript. No ES6+ features like arrow functions, template literals, or classes are used. **Other Alternatives:** 1. **Array.prototype.max()**: In some browsers and engines, you can use `Array.prototype.max()` to find the maximum value in an array. 2. **Lodash library's maxBy() function**: If you prefer a more concise solution, you can use the `maxBy` function from the Lodash library, which finds the maximum value based on a specified function. In conclusion, this benchmark provides a fair comparison of two approaches to finding the maximum value in an array. The results will help users determine which approach is faster and more suitable for their specific use cases.
Related benchmarks:
Get max from an array of numbers (Math.max vs. iteration)
Get max from an array of numbers (Math.max vs. iteration) V2
array math.max (3 variants) vs for loop (4 variants)
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?