var params = [ "hello", true, 7 ]
var other = [ params, 'new' ]
var params = [ "hello", true, 7 ];
params.push('new');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread operator | |
Push |
Test name | Executions per second |
---|---|
spread operator | 9688640.0 Ops/sec |
Push | 75039728.0 Ops/sec |
Let's dive into the benchmark.
Benchmark Purpose: The goal of this benchmark is to compare the performance of two approaches for adding a new element to an array in JavaScript:
push()
method (e.g., params.push('new')
)[...params, 'new']
)Options Compared:
push()
method....
).push()
for large arraysOther Considerations:
Library Used: None. This benchmark is pure JavaScript, without any external libraries or dependencies.
Special JS Feature/Syntax:
The spread operator (...
) is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). It allows for more concise and expressive array creation and manipulation.
Benchmark Preparation Code: The provided JSON defines two test cases:
params
and appending 'new'
.push()
method to append 'new'
to the end of the params
array.Benchmark Results: The latest benchmark results show that:
Overall, these results suggest that the spread operator may be faster than the traditional push()
method for this specific benchmark, but it's essential to consider the specific use case and JavaScript engine being used.