Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Benchmark, spread vs. concat vs. push in Array push item
(version: 0)
spread, concat, push efficiency
Comparing performance of:
spread vs concat vs push
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
spread
let array = []; for(let i = 0; i < 10000; i += 1){ array = [...array, i]; }
concat
let array = []; for(let i = 0; i < 10000; i += 1){ array = array.concat(i); }
push
let array = []; for(let i = 0; i < 10000; i += 1){ array.push(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread
concat
push
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):
The provided JSON represents a benchmark test case on MeasureThat.net, which measures the efficiency of three different methods for pushing items onto an array: `Array.prototype.push()`, `Array.prototype.concat()` with an array literal `[i]` as its second argument (not just concatenating two arrays), and `Array.prototype.push()` followed by spreading an array using the spread operator (`[...array, i]`). Let's break down each method: 1. `Array.prototype.push()`: This is a native JavaScript method that adds one or more elements to the end of an array. It's generally considered the most efficient way to add items to an array. 2. `Array.prototype.concat()`: The second argument passed to `concat()` is expected to be an array. In this case, we're passing `[i]`, which is a new array created on each iteration. This approach can be more memory-intensive than using `push()` because it involves creating a new array and then concatenating the original array with this new one. 3. Spreading (`[...array, i]`): This method uses the spread operator to create a new array by spreading the elements of the original array followed by the item `i`. While this method is often used in more complex scenarios where you need to add an item to an existing array and don't want to modify it directly, its performance characteristics are similar to those of `push()`. **Pros and Cons** - **`Array.prototype.push()`**: Pros: Highly optimized, low memory overhead. Cons: May lead to stack overflows for very large arrays due to the way JavaScript handles function call stacks. - **`Array.prototype.concat()` with an array literal `[i]`**: Pros: Less memory-intensive than using a separate array and `concat()`. However, it creates a new array on each iteration, leading to increased memory usage. Cons: Performance may not be as good as `push()` due to the overhead of creating a new array. - **Spreading (`[...array, i]`)**: Pros: Can be used in more complex scenarios where direct modification is not desirable or when you need to preserve the original order of elements. However, it's less efficient than using `push()` directly. Cons: Increased memory usage due to creating a new array. **Library and Special JS Features** In this benchmark test case, there are no libraries used beyond the standard JavaScript functionality provided by the browser. There are no special JavaScript features or syntax used in this benchmark beyond what is mentioned above (i.e., spread operator). **Alternatives** Other alternatives to `Array.prototype.push()` include: 1. **Using `Unshift()` instead of `Push()`:** If you need to add items to the beginning of an array, `push()` may not be suitable because it modifies the entire array from its end. However, if you want to add items at the beginning, consider using `unshift()`. 2. **Using a more complex data structure:** Depending on your specific use case, creating a custom data structure like a linked list or a doubly-linked list may offer better performance for inserting elements.
Related benchmarks:
Large Array concat vs spread operator vs push
Array concat vs spread operator vs push with more data
Array concat vs spread operator vs push larger list
Large Array concat vs spread operator vs push 300 elements
Array concat vs spread operator vs push + spread 2023-08-21
Comments
Confirm delete:
Do you really want to delete benchmark?