Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
math.max.apply vs math.max(...)
(version: 0)
Comparing performance of:
apply vs spread
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
apply
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; Math.max.apply(null, array);
spread
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; Math.max(...array);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
apply
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
apply
8350489.0 Ops/sec
spread
8122875.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two ways to find the maximum value in an array using JavaScript: `Math.max.apply(null, array)` and `Math.max(...array)`. **Options Compared:** * **`Math.max.apply(null, array)`:** This method uses the `apply` function to call `Math.max` with the array as an argument list. The `null` passed as the first argument indicates that we don't want to use a specific object's context for the function call. * **`Math.max(...array)`:** This method utilizes the spread operator (`...`) to expand the array into individual arguments when calling `Math.max`. **Pros and Cons:** * **`apply`:** * **Pro:** Can be more familiar to developers who learned JavaScript before the widespread adoption of modern syntax like the spread operator. * **Con:** Can be considered slightly less readable compared to the spread operator approach for this specific use case. * **`spread`:** * **Pro:** Cleaner and more concise syntax, often preferred for readability. Directly mirrors the intent of passing elements from an array as arguments. * **Con:** Requires familiarity with modern JavaScript syntax (ES6+). **Other Considerations:** * **Performance:** In this particular case, both methods are likely to perform similarly due to JavaScript's efficient handling of array operations and built-in functions like `Math.max`. The benchmark results you provided might show minor variations, but those could be attributed to browser optimizations or other environmental factors rather than a fundamental difference in the approaches. **Alternatives:** * **`Array.prototype.reduce()`:** You could use the reduce method to find the maximum value iteratively within the array: ```javascript const max = array.reduce((maxVal, current) => (current > maxVal ? current : maxVal), array[0]); ``` * **`Array.prototype.sort()`:** Although not as direct, you could sort the array and take the last element: ```javascript const sortedArray = array.sort((a, b) => a - b); const max = sortedArray[sortedArray.length - 1]; ``` Let me know if you have any other questions or would like to explore specific benchmark results in more detail!
Related benchmarks:
Math.sqrt vs multiply2
toFixed vs toPrecision vs Math.round() vs Math.floorfaster test
toFixed vs Math.round()
toFixed() vs String(Math.floor()
toFixed vs Math.round() with numbers222
Comments
Confirm delete:
Do you really want to delete benchmark?