window.existingArray = ['1','2','3','4','5'];
existingArray.push('6');
existingArray.pop();
window.existingArray = [existingArray, '6'];
existingArray.pop();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Adding a string using Array.push | |
Adding a string using spread |
Test name | Executions per second |
---|---|
Adding a string using Array.push | 781953.4 Ops/sec |
Adding a string using spread | 427667.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark measures the performance difference between two approaches to add a property to an array:
Array.push()
...
)Options Compared
Two options are compared:
Array.push()
to add a new element to the end of the array....
) to create a new array with the original elements and the newly added element.Pros and Cons of Each Approach
Array.push()
for very small arrays, due to the overhead of creating a new array.Library Used
The benchmark uses the existingArray
variable, which is defined in the script preparation code. The existingArray
is an array of strings initialized with five elements: '1'
, '2'
, '3'
, '4'
, and '5'
. This array is used as the input for both benchmarking scenarios.
Special JS Feature/Syntax
There is no special JavaScript feature or syntax being tested in this benchmark. It's a straightforward comparison of two simple programming approaches.
Other Alternatives
While not explicitly mentioned in the benchmark, other alternatives to compare might include:
Array.prototype.concat()
to concatenate the original array with the new element.assignIn
method to add properties to an object (not applicable here, since we're working with arrays).Keep in mind that this benchmark is intended to measure performance differences between two specific approaches, rather than exploring all possible ways to add elements to an array.