Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() vs Array.reduce.apply()
(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.apply(null, values);
Reduce
return values.reduce((prev, curr) => prev > curr ? prev : curr , 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:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.max
69760.7 Ops/sec
Reduce
70725.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is tested on it, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark compares the speed of two approaches: `Math.max()` vs `Array.reduce()`. The test is designed to measure the performance difference between these two methods when applied to an array of values. **Script Preparation Code** The script preparation code generates a large array (`values`) with 5000 elements, where each element is calculated as the remainder of its index divided by 20. This ensures that the array contains a diverse range of values, making it more representative for benchmarking purposes. **Html Preparation Code** There is no HTML preparation code provided, which suggests that this benchmark focuses solely on JavaScript performance and does not take into account other factors like rendering or layout. **Test Cases** The test cases consist of two individual benchmarks: 1. `Math.max()`: This benchmark uses the `Math.max()` function with the `apply()` method to find the maximum value in the `values` array. 2. `Reduce`: This benchmark uses the `reduce()` method to find the maximum value in the `values` array. **Libraries** There are no libraries explicitly mentioned in the provided code. However, it's worth noting that the `Math.max()` function is a built-in JavaScript function, while the `reduce()` method is part of the Array prototype. **Special JS Features/Syntax** The benchmark uses the following special JS features: * The use of arrow functions (`=>`) in the `Reduce` test case. * The `apply()` method in the `Math.max()` test case. These features are not specific to this benchmark and are commonly used in JavaScript development. **Other Alternatives** To achieve similar results, other approaches could be explored: 1. Using `Math.max()` with a custom comparator function instead of `apply()`. 2. Implementing a custom sorting algorithm for the `values` array before applying `reduce()` or `Math.max()`. 3. Using a different data structure, such as an object or a Set, to find the maximum value. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `Math.max()` with `apply()`: Pros - straightforward implementation, good performance for small arrays. Cons - may not be suitable for large arrays due to the overhead of `apply()`. * `Array.reduce()`: Pros - efficient for large arrays, can be easily parallelized. Cons - may require more code and setup compared to `Math.max()`. Ultimately, the choice between these approaches depends on the specific use case, performance requirements, and personal preference. **Benchmark Results** The provided benchmark results show that: * The `Reduce` test case has a higher execution rate (354077.03125 executions per second) compared to the `Math.max()` test case (103321.453125 executions per second). * The results indicate that `Array.reduce()` is generally faster than `Math.max()` for this specific benchmark. Keep in mind that these results may vary depending on the specific hardware, software configuration, and other factors.
Related benchmarks:
Math.max() vs Array.reduce() 4
Math.max vs Array.reduce
Math.max() vs Array.reduce(Math.max)
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?