Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs spread operator (test)
(version: 0)
Comparing performance of:
push vs spread operator
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
push
var other = [ 1, 2 ].push("hello");
spread operator
var other = [ 1, 2, ...["hello"] ]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
push
spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
push
91151688.0 Ops/sec
spread operator
59103852.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and understand what's being tested in this benchmark. **What is being tested?** The provided JSON represents two individual test cases that compare the performance of the `push` operator versus the spread operator (`...`) in JavaScript. The test aims to measure which approach is faster for pushing an element onto a fixed-size array. **Options compared:** There are two options being compared: 1. **Push Operator**: Using the `push()` method to add an element to the end of an array. 2. **Spread Operator**: Using the spread operator (`...`) to create a new array with the original elements and then adding an additional element. **Pros and Cons:** * **Push Operator**: + Pros: Simple, straightforward approach that works well for small arrays or single-element additions. + Cons: May be slower for large arrays due to the overhead of creating a temporary array, even though it's still efficient in most cases. * **Spread Operator**: + Pros: Can be faster for large arrays since it avoids the creation of a temporary array. It also provides more flexibility when working with arrays and objects. + Cons: May be slower for small arrays or single-element additions due to the overhead of creating a new array. **Other considerations:** The choice between `push` and the spread operator depends on the specific use case and performance requirements. For most cases, both approaches will provide acceptable performance. However, if you're working with large datasets or need to optimize for size, the spread operator might be the better choice. **Library: None** There is no external library being used in this benchmark. **Special JS feature/syntax: None** There are no special JavaScript features or syntax being tested in this benchmark. **Benchmark preparation code:** The script preparation code is empty, which means that MeasureThat.net will generate a fresh test environment for each execution. **Individual test cases:** Each test case consists of a single line of JavaScript code: * **Push Operator**: `var other = [ 1, 2 ].push("hello");` * **Spread Operator**: `var other = [ 1, 2, ...["hello"] ];` These lines create an array and push or spread an element onto it. **Latest benchmark result:** The latest benchmark results show that the Chrome 91 browser on a Desktop device executed the `push` test case approximately 71.8 million times per second, while the `spread operator` test case was executed around 53.9 million times per second. Keep in mind that these numbers are specific to this particular benchmark and may not be representative of other scenarios or browsers. **Other alternatives:** If you're interested in exploring alternative approaches, here are a few options: * **Array.prototype.unshift()**: This method adds an element to the beginning of an array. While it might seem similar to the push operator, it's actually a more expensive operation due to the need to shift elements. * **Array.prototype.splice()**: This method modifies an existing array by removing and replacing elements with new content. It can be used in place of `push` or spread operators, but it has a slightly different behavior and might not be as efficient. * **Other libraries or frameworks**: Depending on your specific use case, you might want to explore other JavaScript libraries or frameworks that provide optimized array operations, such as Lodash or Ramda.
Related benchmarks:
spread operator vs push test - correct
spread operator... vs push()
spread vs push test1
JS array spread operator vs push
Array .push() vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?