Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce to array
(version: 0)
Spread accumulator vs push
Comparing performance of:
spread vs push
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
spread
const things = [1, 2, 3, 4]; const reduced = things.reduce((acc, thing) => { return [...acc, thing]; }, []);
push
const things = [1, 2, 3, 4]; const reduced = things.reduce((acc, thing) => { acc.push(thing); return acc; }, []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread
push
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and provide insights on the tested options. **Benchmark Overview** The benchmark measures the performance of two approaches for reducing an array in JavaScript: using `push` versus spreading the accumulator array with `...`. **Options Compared** There are two test cases: 1. **Spread**: This approach uses the spread operator (`...`) to create a new array and append elements to it. ```javascript const reduced = things.reduce((acc, thing) => { return [...acc, thing]; }, []); ``` 2. **Push**: This approach modifies the accumulator array by using `push` to add elements to it. ```javascript const reduced = things.reduce((acc, thing) => { acc.push(thing); return acc; }, []); ``` **Pros and Cons of Each Approach** 1. **Spread**: * Pros: + Creates a new array, which can be more efficient if the original array is large. + Avoids modifying the accumulator array, which can be beneficial for preserving data integrity. * Cons: + Creates an unnecessary copy of the accumulator array, which can lead to increased memory usage. 2. **Push**: * Pros: + Modifies the existing accumulator array, which can reduce memory allocation overhead. * Cons: + Modifies the original array, which can have unintended consequences if the accumulator array is used elsewhere. **Library and Special JS Features** In this benchmark, there are no libraries used, but I'll mention that if any library was used, it would impact the performance results. Additionally, there's a special JS feature being tested: **Rest Parameter Syntax**, which is part of the ECMAScript 2015 (ES6) standard. The Rest Parameter Syntax (`...`) allows you to create an array-like object from arguments, which is what happens in the `spread` approach. This syntax was introduced in ES6 and has since become a standard feature in modern JavaScript. **Other Considerations** * **Array Performance**: The benchmark focuses on reducing an array, but other operations like searching, filtering, or sorting would require different approaches. * **Browser and Platform Variability**: The results are specific to Safari 13 on Mac OS X 10.14.6. Other browsers or platforms might have different performance characteristics. **Alternatives** If you were to write a similar benchmark for reducing an array in JavaScript, you could consider testing: 1. Using `Array.prototype.forEach()` with the `thisArg` parameter. 2. Utilizing Web Workers for parallel execution. 3. Examining the impact of array size on performance using different approaches (e.g., iterating over arrays vs. using `map()` or `filter()`). 4. Measuring performance with various data structures, such as objects or sets. Feel free to ask if you have any further questions or need clarification!
Related benchmarks:
Spread vs mutating
Array spread operator vs push 2
Pushing items via Array.push vs. Spread Operator
Array .push() vs spread operator
Array push vs spread when reducing over results
Comments
Confirm delete:
Do you really want to delete benchmark?