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
llama3.1:latest
, generated one year ago):
Let's dive into the benchmark definition and results. **Benchmark Description** The benchmark compares two different ways to find the maximum value in an array using JavaScript: `Math.max.apply(null, array)` and `Math.max(...array)`. The goal is to measure which method is faster. **Test Cases** There are two test cases: 1. **apply**: This test case uses the `Math.max.apply` method with `null` as its first argument (a technique called "binding") followed by an array of numbers `[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`. The result is the maximum value in the array. 2. **spread**: This test case uses the spread operator (`...`) to pass the entire array as arguments to the `Math.max` function. **Library and Features** In this benchmark, there are no external libraries used. However, I'd like to mention that: * The spread operator (`...`) is a JavaScript feature introduced in ECMAScript 2015 (ES6) that allows an iterable (like an array or string) to be expanded into individual arguments. * `Math.max.apply` uses the `apply` method of the Math object, which takes an array of values and returns the maximum value. **Pros and Cons** Here's a brief comparison of the two methods: * **apply**: This approach binds the first argument (`null`) to the `Math.max` function using `apply`. While it might seem a bit awkward, this method is older (introduced in ES5) and might be supported by older browsers. + Pros: Wide browser support, relatively simple implementation. + Cons: Requires binding of the first argument, which can be confusing. * **spread**: This approach uses the spread operator to pass the entire array as arguments to `Math.max`. + Pros: Clean and concise code, great for modern JavaScript development. + Cons: Only supported in browsers that implemented ES6 (most modern browsers). **Alternatives** If you're looking for alternatives to find the maximum value in an array: * Use the built-in `Array.prototype.reduce` method, which can be more efficient than using `Math.max`. ```javascript const max = array.reduce((a, b) => Math.max(a, b)); ``` * For smaller arrays or specific use cases, consider using a simple loop to find the maximum value. Keep in mind that these alternatives might have their own trade-offs and edge cases. Always profile your code to determine the best approach for your specific use case.
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?