Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Add new element to array: push vs destructuring multiple entries
(version: 0)
Comparing performance of:
spread operator vs Push
Created:
4 years ago
by:
Guest
Go to the latest result
Tests:
spread operator
var params = [ "hello", true, 7 ] var params = [ ...params, 'new' ] var params = [ ...params, 'new' ] var params = [ ...params, 'new' ] var params = [ ...params, 'new' ] var params = [ ...params, 'new' ] var params = [ ...params, 'new' ] var params = [ ...params, 'new' ]
Push
var params = [ "hello", true, 7 ]; params.push('new'); params.push('new'); params.push('new'); params.push('new'); params.push('new'); params.push('new'); params.push('new');
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Push
13.3M/s
spread operator
8.7M/s
View exact numbers
Test name
Executions per second
✓
Push
13,323,293 Ops/sec
spread operator
8,735,712 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking setup. **Benchmark Overview** The benchmark compares two approaches for adding a new element to an array: 1. Using the spread operator (`...`). 2. Using the `push()` method. **Spread Operator Approach** The first test case uses the spread operator approach: ```javascript var params = [ "hello", true, 7 ]; var params = [ ...params, 'new' ]; var params = [ ...params, 'new' ]; var params = [ ...params, 'new' ]; var params = [ ...params, 'new' ]; var params = [ ...params, 'new' ]; var params = [ ...params, 'new' ]; var params = [ ...params, 'new' ]; ``` The spread operator (`...`) is used to create a new array by copying the existing `params` array and adding each of the specified elements. **Push Method Approach** The second test case uses the `push()` method approach: ```javascript var params = [ "hello", true, 7 ]; params.push('new'); params.push('new'); params.push('new'); params.push('new'); params.push('new'); params.push('new'); params.push('new'); ``` This approach modifies the existing `params` array by adding each of the specified elements using the `push()` method. **Comparison and Considerations** Both approaches have their pros and cons: * **Spread Operator Approach:** + Pros: - Efficient in modern browsers, as it uses a single operation to add multiple elements. - Less memory allocation than creating a new array and then pushing elements into it. + Cons: - Requires JavaScript 2015+ syntax (ES6+). - Can be slower due to additional overhead of parsing the spread operator expression. * **Push Method Approach:** + Pros: - Older browsers may have better performance, as they can optimize this simple push operation. - Easier to understand and implement for beginners. + Cons: - Requires multiple `push()` calls, which can lead to slower execution times. **Library Usage** None of the test cases explicitly use a library. However, modern browsers like Safari 17 have built-in optimizations that can affect the performance of these benchmarking tests. **Special JS Features or Syntax** The spread operator approach relies on JavaScript's ES6+ syntax (`...`). This syntax was introduced in 2015 and is widely supported in modern browsers. **Other Alternatives** To test similar benchmarks, you could consider adding additional test cases with different approaches, such as: * Using `concat()` instead of the spread operator. * Modifying an existing array using a loop or other methods (e.g., `Array.prototype.forEach`). * Testing the performance of array literals versus dynamic array creation. Keep in mind that benchmarking should focus on real-world use cases and optimization techniques. These suggestions can help you create more comprehensive benchmarks by exploring different approaches and techniques.
Related benchmarks
Splice vs Spread vs Unshift to insert at beginning of array (fixed from slice)
Javascript Array Spread vs Push
Comments
Confirm delete:
Do you really want to delete benchmark?