Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs push 1232132131
(version: 0)
Comparing performance of:
push vs spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
push
var params = [ "hello", true, 7 ]; var testObj = {a: {b: {c : 'd'}}}; var other = params.push(testObj);
spread
var params = [ "hello", true, 7 ]; var testObj = {a: {b: {c : 'd'}}}; var other = [...params, testObj];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
push
spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 YaBrowser/25.8.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
push
59808384.0 Ops/sec
spread
38419740.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the provided benchmark. **What is being tested?** The benchmark measures the performance difference between using `Array.prototype.push()` and spread syntax (`...`) when adding elements to an array in JavaScript. **Options compared:** Two options are compared: 1. **`Array.prototype.push()`**: This method adds one or more elements to the end of an array and returns the updated length of the array. 2. **Spread syntax (`...`)**: This syntax creates a new array by spreading the elements of an existing array (or other iterable) into a new array. **Pros and Cons:** 1. **`Array.prototype.push()`**: * Pros: Wide support, low overhead, easy to implement. * Cons: Returns the updated length of the array, can be slower for large arrays due to the need to traverse the entire array to find the insertion point. 2. **Spread syntax (`...`)**: * Pros: More expressive, does not return a value, more efficient for large arrays since it avoids traversing the original array. * Cons: Requires JavaScript 2015 (ES6) or later, may be less familiar to some developers. **Library and special JS feature/syntax used:** No libraries are explicitly mentioned in the benchmark. However, note that both `Array.prototype.push()` and spread syntax (`...`) work with modern JavaScript engines, which provide a standard way of working with arrays. **Other considerations:** The test assumes that the input array is large enough to see significant performance differences between the two methods. If the array is small, the performance difference may be negligible. In general, for most use cases, the choice between `Array.prototype.push()` and spread syntax (`...`) should be guided by code readability, maintainability, and familiarity rather than raw performance. **Alternatives:** Other alternatives to compare would include: 1. **`Array.prototype.unshift()`**: Similar to `push()`, but adds elements to the beginning of the array. 2. **`Object.assign()`**: A method that copies the values of an object into another object, which can be used to add elements to an array if needed. In summary, the benchmark measures the performance difference between using `Array.prototype.push()` and spread syntax (`...`) when adding elements to an array in JavaScript. The choice between these two options depends on code readability, maintainability, and familiarity, rather than raw performance.
Related benchmarks:
Intl.NumberFormat vs toLocalString
Intl.NumberFormat vs toLocalString 2
toFixed vs toPrecision vs Math.round() vs Math.floorfast vs new Math.trunc vs numeraljs
Number constructor vs double tilde
Intl.NumberFormat vs toLocalString yoyo
Comments
Confirm delete:
Do you really want to delete benchmark?