var array = [1,2,3];
array.push(5)
array = [array, 5]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array Push | |
Array Spread |
Test name | Executions per second |
---|---|
Array Push | 11980667.0 Ops/sec |
Array Spread | 2.9 Ops/sec |
What is being tested?
The provided JSON represents two individual test cases, each testing the performance of JavaScript arrays. Specifically:
array.push(5)
.[...array, 5]
(using array spread syntax).Comparison options
The provided benchmark compares two approaches to adding an element to an array:
push()
method: This is a built-in JavaScript method that adds one or more elements to the end of an array.Pros and Cons
Array Push:
Pros:
Cons:
Array Spread:
Pros:
push()
when dealing with very large arrays (since it creates a new array without modifying the original)Cons:
Library usage
There is no library used in these benchmark tests.
Special JS feature or syntax
The use of array spread syntax ([...array, 5]
) is a special JavaScript feature introduced in ECMAScript 2015. It allows for more concise and expressive array creation.
Other alternatives
If you want to test other approaches to adding elements to an array, some alternative options could include:
concat()
method: array.concat([5])
var temp = [5]; array.push(temp)
for (var i = 0; i < 1; i++) { array[i] = 5; }
Keep in mind that these alternatives may not be as efficient or readable as the original approaches being tested.
Benchmark preparation code
The provided script preparation code creates an initial array [1,2,3]
, which is used as input for both test cases:
var array = [1,2,3];
This ensures that both test cases start with a consistent starting point.