Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max with apply vs spread vs reduce 10k elements
(version: 0)
Compare the new ES6 spread operator with the Math.max.apply method
Comparing performance of:
Max with apply vs Max with spread operator vs Reduce
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [] for (i = 0; i < 10000; i++) { arr.push(Math.random() * i) }
Tests:
Max with apply
Math.max.apply(Math, arr)
Max with spread operator
Math.max(...arr)
Reduce
arr.reduce((a, b) => Math.max(a, b))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Max with apply
Max with spread operator
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Max with apply
186631.5 Ops/sec
Max with spread operator
15294.1 Ops/sec
Reduce
628.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark is designed to compare the performance of three different methods for finding the maximum value in an array: `Math.max.apply()`, `Math.max()` with the spread operator (`...`), and `reduce()`. **Test Case Breakdown** Each test case is defined by a unique string representing the JavaScript code that will be executed. The three test cases are: 1. **Max with apply**: This test case uses the `Math.max.apply()` method, which applies the `max` function to an array of values. 2. **Max with spread operator**: This test case uses the spread operator (`...`) to pass the entire array as a single argument to the `Math.max()` function. 3. **Reduce**: This test case uses the `reduce()` method to find the maximum value in the array, by applying the `max` function to each element and accumulating the result. **Library/Functionality Used** In this benchmark, only built-in JavaScript functions are used: * `Math.max()`: a global function that returns the largest of zero or more numbers. * `apply()`: a method of the `Function` object that applies a function to an array of arguments. * `reduce()`: a method of the Array prototype that reduces an array to a single value. **Special JS Feature/Syntax** None of the test cases use any special JavaScript features or syntax. They only rely on standard JavaScript functionality. **Options Compared** The benchmark is comparing three different approaches to finding the maximum value in an array: 1. **`Math.max.apply()`**: uses the `apply()` method to apply the `max` function to an array of values. 2. **`Math.max(...)` with spread operator**: uses the spread operator (`...`) to pass the entire array as a single argument to the `max` function. 3. **`reduce()`**: uses the `reduce()` method to find the maximum value in the array. **Pros and Cons** Each approach has its own pros and cons: 1. **`Math.max.apply()`**: * Pros: simple, widely supported, and efficient for small arrays. * Cons: can be slow for large arrays due to the overhead of the `apply()` method. 2. **`Math.max(...)` with spread operator**: * Pros: concise, readable, and efficient for large arrays. * Cons: requires a modern browser that supports the spread operator. 3. **`reduce()`**: * Pros: flexible, can be used for more complex reductions, and can handle arbitrary data structures. * Cons: can be slower than `Math.max.apply()` or `Math.max(...)` due to the overhead of creating a callback function. **Other Alternatives** If you needed to find the maximum value in an array, other alternatives might include: 1. **Using `Array.prototype.indexOf()` and `Array.prototype.lastIndexOf()`**: these methods can be used to find the index of the first occurrence or last occurrence of the maximum value. 2. **Using a custom sorting function with `Array.prototype.sort()`**: this method requires sorting the entire array, which may not be necessary if you only need the maximum value. However, in general, using `Math.max.apply()`, `Math.max(...)` with spread operator, or `reduce()` is the most efficient and concise way to find the maximum value in an array.
Related benchmarks:
Math.max with apply vs spread
Math.max with apply vs spread vs reduce
Math.max.apply vs Math.max spread
Math.max with apply vs spread vs reduce on object array
Comments
Confirm delete:
Do you really want to delete benchmark?