Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Add elements to array: foreach push vs destructuring
(version: 1)
Comparing performance of:
spread operator vs Push
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
spread operator
var params = [ ] var other = ['1', '2', '3'] params.push(...other)
Push
var params = [ ] var other = ['1', '2', '3'] other.forEach(o => params.push(o));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread operator
Push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread operator
33146936.0 Ops/sec
Push
33142902.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described in the provided JSON tests two different approaches to adding elements to an array in JavaScript. The focus is on comparing the efficiency and performance of these two methods: using the spread operator (`...`) versus using the `forEach` method combined with the `push` method. ### Approaches Compared 1. **Spread Operator** - **Test Case:** `params.push(...other)` - **Description:** The spread operator allows an iterable (like an array) to be expanded in places where zero or more arguments are expected. In this case, it expands the `other` array and pushes all its elements into the `params` array in one concise operation. 2. **forEach with Push** - **Test Case:** `other.forEach(o => params.push(o));` - **Description:** This method involves using the `forEach` method to iterate over each element of the `other` array, executing a function for each element that pushes the current element into the `params` array one by one. ### Pros and Cons **Spread Operator:** - **Pros:** - More concise syntax; easier to read and write. - Can be more performant due to less overhead since it operates in a single function call, especially in scenarios with a large number of items. - **Cons:** - In older browsers or environments that do not support ES6 features, this method could lead to compatibility issues. **forEach with Push:** - **Pros:** - Compatible with older versions of JavaScript, ensuring broad compatibility. - Offers more control if additional operations are needed within the loop, such as filtering or transforming elements before pushing them into the new array. - **Cons:** - More verbose and potentially less efficient due to the overhead of calling a function for each element and the repeated `push` operations, especially as the size of the source array increases. ### Other Considerations - When using either method, the size of the arrays can impact performance. The spread operator may have a performance edge in bulk operations, while the `forEach` method can be more versatile if additional logic needs to be included during iteration. - This benchmark runs in Chrome 130 on macOS, which gives specific insights into performance characteristics in that environment, though results can vary based on browser implementations and optimizations. ### Alternatives Other alternatives for adding elements to an array include: - **Array.concat()**: This method can be used to merge two or more arrays without changing the existing arrays, returning a new array. While it can be useful, it is not as efficient as the spread operator for large datasets. - **Using a simple loop (for loop)**: Traditional `for` loops can also be employed to iterate and push elements into a new array. This method, while being the most widely compatible, might not be as expressive as the functional approaches discussed but can be optimized for performance as needed. In summary, this benchmark emphasizes a performance comparison between modern JavaScript syntax (spread operator) and a more traditional approach (forEach with push), helping developers understand which method might be more suitable depending on their project's needs, browser support requirements, and performance considerations.
Related benchmarks:
Add new element to array: push vs destructuring
Add new element to array: push vs destructuring multiple entries
Add new array to array: push vs destructuring
Add new element to array: push vs push with i
Add new element to array: push vs destructuring (array.length of 9)
Add new element to array: push vs destructuring vs directly assign
Add new element to array: push vs destructuring vs indices
Add new array to array: push vs destructuring
Add new element to array: push vs destructuring with Set
Comments
Confirm delete:
Do you really want to delete benchmark?