var arr = Array(100).fill([{n: ''}]);
var params = Array(1000).fill([{n: ''}]);
var other = arr.concat(params);
var params = Array(1000).fill([{n: ''}]);
var other = arr.push(params);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator |
Test name | Executions per second |
---|---|
Array.prototype.concat | 176111.4 Ops/sec |
spread operator | 12465.4 Ops/sec |
Let's dive into explaining the benchmark and its test cases.
Benchmark Overview
MeasureThat.net is used to compare different JavaScript approaches for array concatenation. The provided JSON defines two benchmark test cases: Array.prototype.concat
and spread operator
. The goal is to measure which approach performs better in terms of execution speed, given a large dataset.
Options Compared
The two options being compared are:
Array.prototype.concat()
: This is the traditional method for concatenating arrays in JavaScript. It creates a new array by copying elements from the original array and then adds the elements from the second array....
): This is the newer, more modern approach introduced in ES6 (ECMAScript 2015). It allows you to expand an array into multiple arguments or spread its elements across multiple variables.Pros and Cons of Each Approach
Array.prototype.concat()
:
Pros:
Cons:
Spread Operator (...
):
Pros:
concat()
since it only creates a single new array reference.Cons:
Library Usage
In this benchmark, no external libraries are used. Both Array.prototype.concat()
and the spread operator rely on built-in JavaScript functionality.
Special JS Features/Syntax
The spread operator (...
) is a relatively new feature introduced in ES6 (ECMAScript 2015). It allows you to expand an array into multiple arguments or spread its elements across multiple variables. The syntax arr.push(...params)
uses this feature to add all elements from the params
array to the existing arr
array.
Other Alternatives
If not using the spread operator, other alternatives for concatenating arrays include:
Array.prototype.push()
: Similar to concat()
, but it modifies the original array.Array.prototype.splice()
: Can be used to concatenate two arrays by using the push()
method in a loop.However, these methods are generally less efficient and more cumbersome than using the spread operator or concat()
.