Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.min() vs Array.reduce()
(version: 0)
Compare speed of Math.min() vs Array.reduce().
Comparing performance of:
Math.min 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.min
return Math.min(...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.min
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3424.0 Safari/537.36
Browser/OS:
Chrome 68 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.min
146687.9 Ops/sec
Reduce
67495.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark measures the performance difference between two methods in JavaScript: `Math.min()` and `Array.reduce()`. Both methods are used to find the minimum value in an array. **Options Compared** Two options are compared: 1. **`Math.min()`**: This method directly returns the smallest element from the input array. 2. **`Array.reduce()`**: This method applies a callback function to each element in the array, reducing it to a single output value (in this case, the minimum value). **Pros and Cons of Each Approach** 1. **`Math.min()`**: * Pros: + Efficient, as it only iterates over a subset of elements. + Simple implementation. * Cons: + Only works with arrays that contain at least one element. + Not suitable for non-numerical data types (e.g., strings, objects). 2. **`Array.reduce()`**: * Pros: + Flexible, as it can be used to reduce an array of any type. + Can be easily modified to perform different operations on the elements. * Cons: + Less efficient than `Math.min()` for numerical arrays, as it requires iterating over all elements. + More complex implementation. **Library Used** In this benchmark, the `Array.reduce()` method is used with a custom callback function. The library here is not explicitly mentioned, but the syntax and behavior suggest that it's a standard JavaScript library ( likely ECMAScript). **Special JavaScript Feature/Syntax** The benchmark uses the spread operator (`...`) in the `Math.min()` implementation, which is a relatively recent feature introduced in ECMAScript 2018. This allows for a more concise way of passing multiple arguments to the function. **Other Alternatives** If you need to find the minimum value in an array, other alternatives include: 1. **Using `Math.min()` with a specific set of elements**: Instead of using `Array.reduce()`, you can pass an array of numbers directly to `Math.min()`. For example: `Math.min(10, 20, 30)`. 2. **Using a third-party library**: There are libraries like Lodash or Underscore.js that provide additional utility functions for array operations, including finding the minimum value. 3. **Implementing your own solution**: You can also implement a custom function to find the minimum value in an array, which might be more efficient than using `Array.reduce()` depending on the specific use case. Overall, the choice between `Math.min()` and `Array.reduce()` depends on the specific requirements of your application, such as performance, flexibility, or ease of implementation.
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
for vs new Array.fill
Math.max() vs Array.reduce(Math.max)
Comments
Confirm delete:
Do you really want to delete benchmark?