Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array push an element vs spread (fixed)
(version: 0)
Comparing performance of:
Array.push vs Array spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [100,324,234,234,2,4,4,23,423,42,5,34,63,45,354,23,525,235,23,52,35,235,2,352,35,2,35,235,23,52,53,25,2,35,23,52,35,25235,235,23,52,52,35,235,23,525]
Tests:
Array.push
arr.push(2)
Array spread
arr = [...arr, 2]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.push
Array spread
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 what is tested on the provided JSON benchmark and explain the different approaches compared. **Benchmark Purpose** The primary goal of this benchmark is to compare the performance of two ways to add an element to an array in JavaScript: 1. Using the `push()` method 2. Using the spread operator (`...`) **Options Compared** Two options are being compared: * **Option 1: `arr.push(2)`** + This approach uses the `push()` method, which adds a new element to the end of the array. + The advantage is that it's a simple and well-known operation in JavaScript. + However, it might not be as efficient as other approaches for large arrays or arrays with many elements at the beginning. * **Option 2: `arr = [...arr, 2]`** + This approach uses the spread operator to create a new array by concatenating the original array with the element `[2]`. + The advantage is that it creates a new array and doesn't modify the original one, which can be beneficial for some use cases. + However, it's generally slower than using `push()` because of the overhead of creating a new array. **Pros and Cons** * **`arr.push(2)`** + Pros: - Fast and efficient - Simple and well-known operation + Cons: - May not be as efficient for large arrays or arrays with many elements at the beginning - Modifies the original array * **`arr = [...arr, 2]`** + Pros: - Creates a new array and doesn't modify the original one (in some cases) - Can be beneficial for preserving the integrity of the original data + Cons: - Generally slower than using `push()` - Has higher overhead due to creating a new array **Library Used** None is explicitly mentioned in this benchmark. However, if we look at the "Script Preparation Code", we can see that it's defining a variable `arr` and initializing it with an array of numbers. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark other than the use of spread operator (`...`).
Related benchmarks:
Array: spread operator vs push #2
Array: spread operatorzxc vs push
Array: spread operatorasfasfzxc vs push
Push vs Spread JavaScript
Comments
Confirm delete:
Do you really want to delete benchmark?