Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max() spread vs reduce() vs apply()
(version: 0)
Compare speed of Math.max() apread, reduce() and apply()
Comparing performance of:
Math.max vs Reduce vs Apply
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((a, b) => Math.max(a, b), -Infinity);
Apply
function getMaxOfArray(numArray) { return Math.max.apply(null, numArray); } return getMaxOfArray(values);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.max
Reduce
Apply
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.max
36183.6 Ops/sec
Reduce
20825.3 Ops/sec
Apply
35752.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The benchmark tests three different ways to find the maximum value in an array: 1. `Math.max() spread syntax`: This method uses the `...` operator to pass the array elements as separate arguments to `Math.max()`. 2. `Array.prototype.reduce()` method: This method applies a reduction function to the array, which returns the first value that satisfies the condition (in this case, finding the maximum value). 3. `Function.prototype.apply()` method: This method calls a function with an array of arguments. **Options Compared** The benchmark compares the performance of these three methods on an array of 5000 elements, where each element is a random integer between 0 and 19. **Pros and Cons of Each Approach** 1. **Math.max() spread syntax**: * Pros: concise and expressive, easy to read. * Cons: may not be optimized for performance in older browsers or with large arrays. 2. **Array.prototype.reduce()` method: * Pros: flexible and powerful, can be used for other reduction operations. * Cons: slower than `Math.max()` spread syntax due to the overhead of calling a function. 3. **Function.prototype.apply()` method: * Pros: can be used with any function, not just `Math.max()`. * Cons: slower than `Math.max()` spread syntax and may have performance issues with large arrays. **Library Used** None explicitly mentioned in this benchmark. However, the use of `Array.prototype.reduce()` implies that the browser has a compatible implementation of this method. **Special JS Features or Syntax** The only special feature used here is the `...` operator in the spread syntax, which was introduced in ECMAScript 2015 (ES6). This allows function calls to accept an arbitrary number of arguments using the `...` operator. The use of this syntax suggests that the benchmark was run on a modern browser or with a compatible implementation. **Other Considerations** The benchmark uses a small array size and random values to minimize the impact of caching or other performance effects. However, in practice, these methods may have different performance characteristics for larger arrays or more complex data structures. If you were to rewrite this benchmark, you might consider using a larger array size and more diverse data types to test the performance of these methods in more realistic scenarios. **Alternatives** Some alternative methods for finding the maximum value in an array include: 1. Using `Math.max()` with an arrow function (e.g., `Math.max((a, b) => a > b ? a : b)`). 2. Using a simple loop to iterate over the array and return the first element greater than or equal to all others. 3. Using a library like Lodash, which provides a `max` function that can be used in place of `Math.max()`. Keep in mind that these alternatives may have different performance characteristics than the methods tested in this benchmark.
Related benchmarks:
math.max.apply vs math.max(...)
Javascript: reduce VS for with Math.max
Math.max() without spread vs reduce vs apply
array math.max (3 variants) vs for loop (4 variants)
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?