let array = [1,2,3];
array = [array, 4];
let array = [1,2,3];
array.push(4)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread | |
Push |
Test name | Executions per second |
---|---|
Spread | 49814516.0 Ops/sec |
Push | 76966912.0 Ops/sec |
Let's dive into the explanation of the provided benchmark.
What is being tested?
The provided benchmark tests two approaches to add an element to an array:
...
) to create a new array and add the element 4
to it, assigning the result back to the original array
.push()
method to add the element 4
to the end of the original array
.Options compared
The benchmark compares two approaches:
...
operator)array.push(4)
)Pros and Cons of each approach:
Spread syntax (...
operator)
push()
.Push method (array.push(4)
)
Other considerations:
Alternatives:
If you're interested in exploring alternative approaches, consider:
unshift()
instead of push()
, which adds an element to the beginning of the array.Benchmark preparation code and JSON
The provided JSON defines the benchmark:
{
"Name": "Spread vs Push when adding into array",
"Description": null,
"Script Preparation Code": null,
"Html Preparation Code": null
}
And it contains individual test cases in the following format:
[
{
"Benchmark Definition": "let array = [1,2,3];\r\narray = [...array, 4];",
"Test Name": "Spread"
},
{
"Benchmark Definition": "let array = [1,2,3];\r\narray.push(4)",
"Test Name": "Push"
}
]
Latest benchmark result
The provided latest benchmark result:
[
{
"RawUAString": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Browser": "Chrome 120",
"DevicePlatform": "Desktop",
"OperatingSystem": "Mac OS X 10.15.7",
"ExecutionsPerSecond": 76966912.0,
"TestName": "Push"
},
{
"RawUAString": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Browser": "Chrome 120",
"DevicePlatform": "Desktop",
"OperatingSystem": "Mac OS X 10.15.7",
"ExecutionsPerSecond": 49814516.0,
"TestName": "Spread"
}
]