Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() vs Array.reduce(Math.max)
(version: 0)
Compare speed of Math.max() vs Array.reduce(Math.max).
Comparing performance of:
Math.max vs Reduce
Created:
one year 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((accumulator, currentValue) => Math.max(accumulator, currentValue), 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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.max
133207.9 Ops/sec
Reduce
3073.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested, compared, and analyzed. **What is being tested?** The test case measures the performance difference between two approaches: 1. `Math.max()` function 2. `Array.prototype.reduce()` method with a callback function that always returns `Math.max()` **Options compared:** Two options are being compared: A) Using the built-in `Math.max()` function B) Using the `Array.prototype.reduce()` method with a custom callback function that always returns `Math.max()` **Pros and Cons of each approach:** 1. **`Math.max()`**: * Pros: Simple, efficient, and widely supported. * Cons: Can be slower for large arrays due to overhead from JavaScript's built-in functions. 2. **`Array.prototype.reduce()` with custom callback**: * Pros: Allows for more control over the reduction process, can be faster for large arrays since it doesn't involve an additional function call. * Cons: Requires a custom callback function that always returns `Math.max()`, which might be less readable and maintainable. The test results suggest that using `Array.prototype.reduce()` with a custom callback is generally faster than using the built-in `Math.max()` function, especially for large arrays like the one in this benchmark (5000 elements). **Library usage:** None of the provided code uses any external libraries. The `Math` object and `Array` prototype are part of the JavaScript standard library. **Special JS features or syntax:** No special JavaScript features or syntax are being used in this test case. It's purely functional programming with a focus on performance comparison. **Alternative approaches:** Other alternatives to compare performance include: * Using `Math.min()` instead of `Math.max()` * Using a different reduction algorithm, such as using `Array.prototype.forEach()` and accumulating the maximum values manually * Comparing the performance of other languages or implementations (e.g., Node.js, TypeScript) * Using different array sizes or distributions to test the performance under various scenarios
Related benchmarks:
Math.max() vs Array.reduce() 4
Math.max vs Array.reduce
Get max from an array of numbers (Math.max vs. iteration) V3
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?