Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ES6 spread operator vs. Array.prototype.reduce()
(version: 0)
Comparing performance of:
Spread operator vs Array.prototype.reduce()
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myArray = [27, 11, 46, 64, 62, 42, 5, 9];
Tests:
Spread operator
Math.max(...myArray);
Array.prototype.reduce()
myArray.reduce((a, b) => Math.max(a, b))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread operator
Array.prototype.reduce()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread operator
35171576.0 Ops/sec
Array.prototype.reduce()
81173072.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmark on MeasureThat.net. The benchmark tests two approaches for finding the maximum value in an array: the spread operator and Array.prototype.reduce(). **Test Case 1: Spread Operator** The test case uses the following code: ```javascript Math.max(...myArray); ``` This line of code uses the spread operator (`...`) to pass all elements of the `myArray` array as separate arguments to the `Math.max()` function. This approach is a concise and readable way to find the maximum value in an array. Pros: * Concise and easy to read * Does not require iterating over the entire array Cons: * May incur additional overhead due to creating temporary arrays * May not be as efficient as other approaches for very large datasets **Test Case 2: Array.prototype.reduce()** The test case uses the following code: ```javascript myArray.reduce((a, b) => Math.max(a, b)); ``` This line of code uses the `reduce()` method to iterate over the elements of the `myArray` array and find the maximum value. The callback function `(a, b) => Math.max(a, b)` is called for each pair of elements in the array, where `a` and `b` are consecutive elements. Pros: * Efficient for large datasets, as it only requires a single pass over the array * Does not require creating temporary arrays Cons: * May be less readable than other approaches due to the use of an anonymous callback function * Requires understanding of the `reduce()` method's behavior **Library and Special JS Feature** Neither of these test cases uses any libraries or special JavaScript features. The spread operator is a built-in feature introduced in ES6, while Array.prototype.reduce() is also a built-in method. **Other Alternatives** There are other approaches to finding the maximum value in an array, such as: * Using `Math.max.apply(null, myArray)` (which is similar to the spread operator but uses the `apply()` method instead) * Using `myArray[0]` and iterating over the rest of the array using a for loop * Using `for...of` loops to iterate over the elements of the array However, these alternatives are generally less efficient or more verbose than the Array.prototype.reduce() approach. **Benchmark Results** The benchmark results show that the spread operator is slower than Array.prototype.reduce() in this particular test case. The spread operator executes at an average rate of 566 executions per second, while Array.prototype.reduce() executes at an average rate of 1,444,566 executions per second. This suggests that for large datasets, Array.prototype.reduce() may be a more efficient approach.
Related benchmarks:
ES6 spread operator vs. Array.prototype.reduce()
ES6 spread operator vs. Array.prototype.reduce()
Array.prototype.slice vs spread operator on a bigger array
Array.prototype.slice vs spread operator - large array 100000
Comments
Confirm delete:
Do you really want to delete benchmark?