Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max with apply vs spread
(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
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [] for (i = 0; i < 1000; i++) { arr.push(Math.random() * i) }
Tests:
Max with apply
Math.max.apply(Math, arr)
Max with spread operator
Math.max(...arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Max with apply
Max with spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Max with apply
146315968.0 Ops/sec
Max with spread operator
51231.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **What's being compared?** The test is comparing two approaches for finding the maximum value in an array: `Math.max.apply()` and the new ES6 spread operator (`...`). **Options being compared:** 1. **`Math.max.apply(Math, arr)`**: This method uses the `apply()` function to call `Math.max` with the `arr` array as its first argument and `Math` as its second argument (the context). The result is passed back through the callback function. 2. **`Math.max(...arr)`**: This approach uses the spread operator (`...`) to pass the entire array to `Math.max`. **Pros and cons of each approach:** 1. **`Math.max.apply(Math, arr)`**: * Pros: + Works in older browsers that don't support the spread operator. + Can be useful for legacy code or compatibility reasons. * Cons: + Has a slightly higher overhead due to the `apply()` function call. 2. **`Math.max(...arr)`**: * Pros: + More concise and readable than the `apply()` version. + Optimized by modern browsers for performance. * Cons: + May not work in older browsers that don't support the spread operator. **Library usage:** None of these benchmarks use a library. **Special JS feature/syntax:** The benchmark uses the ES6 spread operator (`...`). While this is a standard feature in modern JavaScript, it's still possible to encounter older browsers or environments that may not support it. MeasureThat.net takes care to test both approaches for compatibility. **Other alternatives:** Before `Math.max.apply()`, developers used various other methods to find the maximum value in an array, such as: 1. **`Math.max()` with multiple arguments**: This approach involves passing all elements of the array as separate arguments to `Math.max`. For example: `Math.max(...arr)`. 2. **Custom loops**: Before the spread operator was introduced, developers would use custom loops to find the maximum value in an array. However, these alternatives are less efficient and more verbose than using `Math.max()` with either the spread operator or `apply()`.
Related benchmarks:
Math.max with apply vs spread vs reduce
Math.max.apply vs Math.max spread
Math.max with apply vs spread vs reduce 10k elements
Math.max with apply vs spread vs reduce on object array
Comments
Confirm delete:
Do you really want to delete benchmark?