Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
1 Math.min() + 1 Math.max() vs 1 Array.reduce()
(version: 0)
Compare speed of executing Math.min() and Math.max() for one array vs Array.reduce().
Comparing performance of:
Math.min() and Math.max() vs Reduce
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = new Array(10000); for (let i = 0; i < values.length; ++i) { values[i] = i % 20; }
Tests:
Math.min() and Math.max()
const min = Math.min(...values); const max = Math.max(...values); return {min, max}
Reduce
return values.reduce( (acc, value ) => { if (acc.min > value) acc.min = value if (acc.max < value) acc.max = value return acc }, { min: Infinity, max: 0 } )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.min() and 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/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.min() and Math.max()
36487.9 Ops/sec
Reduce
16788.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The benchmark compares the speed of executing `Math.min()` and `Math.max()` functions for one array using two different approaches: 1. **Direct function calls**: The first approach uses direct function calls, where `Math.min` and `Math.max` are called separately on the entire array. 2. **Array reduce method**: The second approach uses the `reduce` method of the Array prototype to find the minimum and maximum values in a single operation. **Options compared** The benchmark compares two options: * Direct function calls (`Math.min()` and `Math.max()`) * Array reduce method (`values.reduce()`) **Pros and cons of each approach** 1. **Direct function calls** * Pros: + Easy to understand and implement + No additional library or framework dependencies required * Cons: + May be slower due to the overhead of function calls + Requires explicit looping to find the minimum and maximum values 2. **Array reduce method** * Pros: + More concise and readable code + Can be faster due to the optimized implementation of `reduce` in modern browsers * Cons: + May require additional library or framework dependencies (not explicitly mentioned here) + Less intuitive for beginners, as it relies on a specific method rather than direct function calls **Library and syntax** In this benchmark, no special JavaScript libraries or syntax are used. The code is straightforward and uses built-in functions. **Other alternatives** If you were to implement this benchmark yourself, other options could include: * Using `forEach` instead of `reduce` * Implementing a custom loop to find the minimum and maximum values * Using a different optimization technique, such as caching or memoization Keep in mind that these alternatives might not be directly comparable to the direct function call and array reduce method approaches used in this benchmark. **Benchmark preparation code** The script preparation code generates an array of 10,000 random integers and assigns each value to the `values` array. This allows the benchmark to run consistently across different executions. The HTML preparation code is empty, indicating that no additional setup or configuration is required for the HTML document.
Related benchmarks:
Math.max() vs Array.reduce.apply()
Math.max() vs Array.reduce() 4
Math.max() vs Array.reduce() vs for element of array loop
Math.max() vs Array.reduce() vs For-loop
Math.max() vs Array.reduce(Math.max)
Comments
Confirm delete:
Do you really want to delete benchmark?