const array = new Array(100)
const value = "Addition"
let newArray = [array, value]
const array = new Array(100)
const value = "Addition"
let newArray = array.push(value)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread | |
Push |
Test name | Executions per second |
---|---|
Spread | 5261800.5 Ops/sec |
Push | 11942829.0 Ops/sec |
Let's break down the benchmark test case.
Benchmark Name: Spread Operator vs Push Method
Description: This test compares the performance of two methods to add an element to the end of an array: using the spread operator ([...]
) and using the push()
method.
Test Cases:
const array = new Array(100) const value = "Addition" let newArray = [...array, value]
* This test uses the spread operator to create a new array by concatenating the original `array` with the `value`.
2. **Push**
* Code:
```javascript
const array = new Array(100)
const value = "Addition"
let newArray = array.push(value)
* This test uses the `push()` method to add the `value` to the end of the `array`, and returns the new length of the array.
Latest Benchmark Results:
The test results show that, on average:
What's being tested:
In this benchmark, we're testing how quickly each method can add an element to the end of a large array (100 elements). The results show that using the spread operator is significantly faster than using the push()
method.
Pros and Cons:
Other Considerations:
push()
and the spread operator may be negligible.push()
repeatedly.Library/Feature Used:
None, this test uses only standard JavaScript features.
Alternatives:
Other ways to add an element to the end of an array include:
concat()
method:
let newArray = array.concat([value]);
2. Using a loop to push elements individually:
```javascript
for (let i = 0; i < 100; i++) {
array.push(value);
}
Note that these alternatives may have their own performance trade-offs and are not recommended as substitutes for the spread operator or push()
method in most cases.