Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.max with apply vs spread vs reduce on object array
(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:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [] for (i = 0; i < 50000; i++) { arr.push({ value: Math.random() * i }) }
Tests:
Max with apply
Math.max.apply(Math, arr.map(e=>e.value))
Max with spread operator
Math.max(...arr.map(e=>e.value))
Reduce
arr.reduce((a, b) => Math.max(a, b.value), 0)
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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Max with apply
1313.3 Ops/sec
Max with spread operator
1325.1 Ops/sec
Reduce
439.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks. **Benchmark Overview** The provided benchmark compares three approaches to find the maximum value in an array: `Math.max.apply`, `Math.max` with the spread operator (`...`), and the `reduce` method. The benchmark is designed to test the performance of these methods on a large array of random values. **Benchmark Definition JSON** The benchmark definition JSON contains the following information: * **Name**: A unique name for the benchmark. * **Description**: A brief description of the benchmark. * **Script Preparation Code**: The code that prepares the input data for the benchmark. In this case, it creates an array of 50,000 objects with random `value` properties. * **Html Preparation Code**: An empty string, indicating that no HTML preparation is required. **Test Cases** The benchmark consists of three individual test cases: 1. **Max with apply**: Tests `Math.max.apply(Math, arr.map(e=>e.value))`. 2. **Max with spread operator**: Tests `Math.max(...arr.map(e=>e.value))`. 3. **Reduce**: Tests `arr.reduce((a, b) => Math.max(a, b.value), 0)`. **Library and Syntax** The benchmark uses the following libraries and syntax: * No external libraries are required for this benchmark. * The `map` method is used to transform the array of objects into an array of values. This method returns a new array with the same elements as the original array, but with each element transformed according to the provided callback function. * The spread operator (`...`) is used to pass the array of values to `Math.max`. * The `reduce` method is used to find the maximum value in the array. This method applies a reducer function to each element in the array, accumulating a result. **Pros and Cons** Here's a brief analysis of the pros and cons of each approach: 1. **Max with apply**: * Pros: Can be more flexible than other approaches since it accepts any number of arguments. * Cons: Can be slower due to the overhead of passing multiple arguments to `apply`. 2. **Max with spread operator**: * Pros: More concise and readable than using `apply`. * Cons: May not work as expected if the callback function returns multiple values (since only one value can be passed to `max`). 3. **Reduce**: * Pros: Can handle arrays of any size, and is often faster due to its optimized implementation. * Cons: Requires more memory to store the accumulator result. **Other Alternatives** If you're looking for alternative approaches to find the maximum value in an array, consider using: 1. `Math.max(...array)`: A concise and readable way to find the maximum value in an array (if the array is small enough to fit on the stack). 2. `Array.prototype.every` with a callback function that returns true if the element is less than or equal to the accumulator result. 3. A custom implementation using bitwise operations, which can be faster for very large arrays. Keep in mind that the performance of these alternatives may vary depending on the specific use case and hardware.
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 10k elements
Comments
Confirm delete:
Do you really want to delete benchmark?