Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() vs Array.reduce()
(version: 0)
Compare speed of Math.max() vs Array.reduce().
Comparing performance of:
Math.max vs Reduce
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = new Array(5000); for (let i = 0; i < values.length; ++i) { values[i] = i % 20; }
Tests:
Math.max
return Math.max(...values);
Reduce
return values.reduce((prev, curr) => { if (curr > prev) return curr; return prev; }, 0);
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:
5 hours ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.max
134254.1 Ops/sec
Reduce
819485.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and the pros and cons of each approach. **What's being tested?** The benchmark compares the speed of two approaches: `Math.max()` and `Array.reduce()` for finding the maximum value in an array. The test case uses a large array of 5000 elements, where each element is assigned a random value between 0 and 19 (inclusive). **Options compared:** 1. **Math.max()**: This function returns the largest of zero or more numbers. 2. **Array.reduce()**: This method applies a reduction function to all elements in an array. **Pros and cons of each approach:** * **Math.max():** + Pros: - Simple, straightforward implementation. - Fast execution since it's a built-in function optimized for performance. + Cons: - May not be suitable for custom reduction functions or complex logic. - Not designed to handle large arrays efficiently (it uses a brute-force approach). * **Array.reduce():** + Pros: - Flexible and reusable for various reduction tasks. - Can handle large arrays efficiently using a accumulator-based approach. + Cons: - May require more code and complexity for simple use cases. - Not optimized for performance like Math.max(). **Library/Function used:** None explicitly mentioned in the provided JSON. However, `Array.reduce()` is a standard JavaScript method that's widely supported across modern browsers and Node.js environments. **Special JS feature/syntax:** The test case uses no special JavaScript features or syntax beyond what's standard for ES5 compatibility. **Other considerations:** * The benchmark focuses solely on the speed comparison between Math.max() and Array.reduce(). It doesn't account for other factors like code readability, maintainability, or error handling. * The random array generation might not accurately represent real-world scenarios where data is generated in different ways (e.g., user input, file I/O). **Alternatives:** If you want to explore alternative approaches for finding the maximum value in an array: 1. **Using a custom function**: Create a reusable function that iterates over the array and returns the maximum value. 2. **Using `Array.prototype.max()`**: Some modern browsers support this polyfill, which provides a more efficient implementation of Math.max() on arrays. Keep in mind that these alternatives might not provide significant performance benefits compared to the built-in methods or Array.reduce().
Related benchmarks:
Math.max() vs Array.reduce.apply()
Math.max() vs Array.reduce() 4
Math.max vs Array.reduce
Get max from an array of numbers (Math.max vs. iteration) V3
Math.max() vs Array.reduce(Math.max)
Comments
Confirm delete:
Do you really want to delete benchmark?