Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array spread operator vs push in reduce vs concat
(version: 0)
Compare the new ES6 spread operator with push inside reduce vs concat
Comparing performance of:
spread operator vs Push vs Concat
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
spread operator
var arr = [ { "opportunityId": 5943111, "findingList": [ { "findingId": 6919 } ], }, { "opportunityId": 5943111, "findingList": [ { "findingId": 6918, "findingId": 6917 } ] } ] arr.reduce((acc, findingInOpp) => [...acc, ...findingInOpp.findingList.map(({ findingId }) => findingId)],[])
Push
var arr = [ { "opportunityId": 5943111, "findingList": [ { "findingId": 6919 } ], }, { "opportunityId": 5943111, "findingList": [ { "findingId": 6918, "findingId": 6917 } ] } ] arr.reduce((acc, findingInOpp) => { acc.push(...findingInOpp.findingList.map(({ findingId }) => findingId)) return acc },[])
Concat
var arr = [ { "opportunityId": 5943111, "findingList": [ { "findingId": 6919 } ], }, { "opportunityId": 5943111, "findingList": [ { "findingId": 6918, "findingId": 6917 } ] } ] arr.reduce((acc, findingInOpp) => acc.concat(findingInOpp.findingList.map(({ findingId }) => findingId)),[])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread operator
Push
Concat
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):
Measuring performance differences is crucial in software development, and Benchmarking tools like MeasureThat.net help you do just that. Now, let's dive into the benchmark: **Benchmark Definition** The benchmark compares three approaches for flattening an array of objects with finding IDs: 1. Using the `Array.prototype.push` method inside a `reduce` function. 2. Using the new ES6 spread operator (`...`) to create a new array. 3. Using `Array.prototype.concat`. **Options Compared** Here's what each option does: 1. **Push**: Iterates over the `findingList` array and pushes each finding ID into the accumulator array using `push`. This approach modifies the original array. 2. **Spread Operator (`...`)**: Creates a new array by spreading the elements of the `findingList` array into it, effectively flattening it. This approach creates a new array without modifying the original one. 3. **Concat**: Concatenates the arrays using `concat`, which also creates a new array. **Pros and Cons** 1. **Push**: * Pros: Simple to implement, may be faster since it modifies the original array (reduces memory allocation). * Cons: Modifies the original array, can lead to unexpected side effects. 2. **Spread Operator (`...`)**: * Pros: Creates a new array without modifying the original one, concise syntax. * Cons: May incur additional overhead for creating a new array. 3. **Concat**: * Pros: Also creates a new array without modifying the original one, can be more readable. * Cons: May incur additional overhead for concatenating arrays. In general, `push` and `concat` are faster since they avoid creating a new array, but modify the original one. The spread operator is more concise but may incur additional overhead due to its nature of creating a new array. **Library Used** None in this benchmark definition. **Special JS Features or Syntax** The benchmark uses modern JavaScript features: 1. ES6 spread operator (`...`). 2. `Array.prototype.reduce`. 3. Modern array methods (e.g., `concat`, `push`). Other Alternatives If you want to explore other approaches, consider these alternatives: * Using a library like Lodash or Ramda for array manipulation. * Implementing a custom flattening function using recursion or iteration. * Using a different data structure, such as a Set or Map, to store the finding IDs. Keep in mind that each approach has its trade-offs and may be more suitable depending on your specific use case.
Related benchmarks:
Array.prototype.concat vs spread operator
Array.prototype.concat vs Spread operator
concat 2 arrays: Array.prototype.concat vs spread operator
Array concat vs spread operator vs push with more data
Array.prototype.concat vs spread operator real
Comments
Confirm delete:
Do you really want to delete benchmark?