Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs Push when adding into array
(version: 0)
Comparing performance of:
Spread vs Push
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
let array = [1,2,3]; array = [...array, 4];
Push
let array = [1,2,3]; array.push(4)
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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
49814516.0 Ops/sec
Push
76966912.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The provided benchmark tests two approaches to add an element to an array: 1. **Spread syntax**: The first test case uses the spread operator (`...`) to create a new array and add the element `4` to it, assigning the result back to the original `array`. 2. **Push method**: The second test case uses the `push()` method to add the element `4` to the end of the original `array`. **Options compared** The benchmark compares two approaches: * Spread syntax (`...` operator) * Push method (`array.push(4)`) **Pros and Cons of each approach:** 1. **Spread syntax (`...` operator)** * Pros: * More concise and expressive than using `push()`. * Can be more efficient in certain cases, as it avoids the overhead of a function call. * Cons: * May have performance implications when dealing with large arrays or high-speed iterations. 2. **Push method (`array.push(4)`)** * Pros: * Widely supported and well-established. * Can be more predictable in terms of performance, as it's a built-in method with a clear behavior. * Cons: * Less concise than the spread syntax. * May have overhead due to the function call. **Other considerations:** 1. **Library usage**: The test case does not explicitly use any external libraries or frameworks. 2. **Special JS features or syntax**: Neither of the two approaches uses special JavaScript features or syntax beyond the standard ECMAScript language. **Alternatives:** If you're interested in exploring alternative approaches, consider: 1. Using `unshift()` instead of `push()`, which adds an element to the beginning of the array. 2. Employing a different data structure, such as an object or a set, if performance-critical code requires efficient insertion operations. **Benchmark preparation code and JSON** The provided JSON defines the benchmark: ```json { "Name": "Spread vs Push when adding into array", "Description": null, "Script Preparation Code": null, "Html Preparation Code": null } ``` And it contains individual test cases in the following format: ```json [ { "Benchmark Definition": "let array = [1,2,3];\r\narray = [...array, 4];", "Test Name": "Spread" }, { "Benchmark Definition": "let array = [1,2,3];\r\narray.push(4)", "Test Name": "Push" } ] ``` **Latest benchmark result** The provided latest benchmark result: ```json [ { "RawUAString": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "Browser": "Chrome 120", "DevicePlatform": "Desktop", "OperatingSystem": "Mac OS X 10.15.7", "ExecutionsPerSecond": 76966912.0, "TestName": "Push" }, { "RawUAString": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", "Browser": "Chrome 120", "DevicePlatform": "Desktop", "OperatingSystem": "Mac OS X 10.15.7", "ExecutionsPerSecond": 49814516.0, "TestName": "Spread" } ] ```
Related benchmarks:
Pushing items via Array.push vs. Spread Operator
Javascript: Spread vs push
JS array spread operator vs push
Array .push() vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?