Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Push vs Concat vs Spread Operator
(version: 0)
Comparing performance of:
Push vs Concat vs Spread Operator
Created:
6 years ago
by:
Registered User
Jump to the latest result
Tests:
Push
var arr = [ "hello", true, 7 ]; arr.push(1);
Concat
var arr = [ "hello", true, 7 ]; arr = arr.concat(1);
Spread Operator
var arr = [ "hello", true, 7 ]; arr = [...arr, 1];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Push
Concat
Spread Operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared, and considered. **What is being tested?** The benchmark measures the performance of three different approaches to add an element to an array: 1. `arr.push(1)`: The built-in `push` method. 2. `arr.concat(1)`: The `concat` method. 3. `arr = [...arr, 1]`: The spread operator ( introduced in ECMAScript 2015). **Options compared** The benchmark compares the performance of these three approaches on an array with a fixed size of 3 elements (`var arr = [ "hello", true, 7 ];`). The approach being tested is identified by its `Test Name` in the individual test cases. **Pros and Cons of each approach:** 1. **Built-in `push` method:** * Pros: + Fastest execution time (as shown in the benchmark results). + Widely supported across browsers and platforms. * Cons: + May not be as efficient for very large arrays, due to its internal implementation details. 2. **`concat` method:** * Pros: + Allows for flexibility in adding elements to an array (e.g., using `concat([1, 2, 3])`). * Cons: + Generally slower than the `push` method due to its internal creation of a new array. 3. **Spread operator (`arr = [...arr, 1]`):** * Pros: + Allows for concise and readable code (e.g., `arr = [...arr, 1];` is often preferred over `arr.push(1)`). * Cons: + May incur a small performance overhead due to the creation of a new array. **Library usage** None of the test cases explicitly uses any libraries or external dependencies. The benchmark only relies on built-in JavaScript features and arrays. **Special JS feature/syntax** The `spread operator` (`arr = [...arr, 1]`) is a special syntax introduced in ECMAScript 2015. It allows for expanding an array-like object into a new array. **Alternative approaches** Other alternatives to these three approaches include: * Using the `unshift()` method instead of `push()`. * Using the `splice()` method with negative indices to add elements at the beginning of the array. * Creating a new array using the `Array.from()` method and then assigning it to the original variable. These alternative approaches may offer trade-offs in terms of performance, code readability, or platform support. However, they are not explicitly compared in this benchmark. In summary, the benchmark measures the performance of three common ways to add an element to an array: built-in `push` method, `concat` method, and spread operator. The results provide insight into the relative performance of these approaches across different browsers and platforms.
Related benchmarks:
spread operator vs push Brian
spread operator vs push Brian2
Array concat vs spread operator vs push with more data
Array concat vs spread operator vs push larger list
zk test spread vs push
Comments
Confirm delete:
Do you really want to delete benchmark?