let array1 = ['a', 'b', 'c'];
array1.push('d');
let array1 = ['a', 'b', 'c'];
array1.concat('d');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array#push | |
Array#concat |
Test name | Executions per second |
---|---|
Array#push | 128624520.0 Ops/sec |
Array#concat | 20799118.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
What is being tested?
The benchmark compares two ways to append an element to an array in JavaScript:
Array#push
: This method appends one or more elements to the end of an array.Array#concat
: This method creates a new array by concatenating another array (or values) with the existing array.Options being compared
The benchmark is comparing the performance of these two approaches:
Array#push
Array#concat
Pros and cons of each approach
Array#push
:Array#concat
:Array#push
, as it allows appending multiple values without specifying their count.Array#push
, especially for large arrays.Other considerations
Library usage (none in this example)
There are no libraries mentioned in this benchmark definition.
Special JS features or syntax
The benchmark uses a feature-specific syntax for the Array#push
and Array#concat
methods, which may be considered outdated. However, it's still widely supported in modern browsers.
Alternative approaches
If you're looking for alternative ways to append elements to an array, some other options include:
...array
) with push()
: array.push(...newArray)
unshift()
or splice()
instead of push()
Array.from()
method and pushing elements onto it: let newArray = Array.from([], (value) => value); newArray.push(element)