var params = [ { 'ten': 10 }, { 'six': 6 }, {'nested': { 'five': 5 }} ];
var other = [ { 'one': 1 }, { 'two': 2 } ].concat(params);
var params = [ { 'ten': 10 }, { 'six': 6 }, {'nested': { 'five': 5 }} ];
var other = [ params, { 'one': 1 }, { 'two': 2 } ];
var params = [ { 'ten': 10 }, { 'six': 6 }, {'nested': { 'five': 5 }} ];
var other = [ { 'one': 1 }, { 'two': 2 }, params ];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator (spread at head) | |
spread operator (spread at tail) |
Test name | Executions per second |
---|---|
Array.prototype.concat | 17643870.0 Ops/sec |
spread operator (spread at head) | 23375752.0 Ops/sec |
spread operator (spread at tail) | 23330750.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, including the provided benchmark: "Array.prototype.concat vs spread operator - with Arrays of Objects" (Updated Oct 2020).
Tested Options
The benchmark compares three different approaches for concatenating two arrays in JavaScript:
Array.prototype.concat()
: The traditional method using the concat()
method....params
): Using the spread operator to add elements to the beginning of the array....params
): Using the spread operator to add elements to the end of the array.Pros and Cons of Each Approach
Array.prototype.concat()
:...params
):concat()
when the spread operator is implemented efficiently by the browser engine.concat()
....params
):concat()
under similar circumstances as spread at head.Libraries Used
None of the benchmark definitions explicitly use any external libraries. However, it's worth noting that modern browsers have built-in support for the spread operator, which was introduced in ECMAScript 2018 (ES2018).
Special JavaScript Features/Syntax
...
), introduced in ECMAScript 2015 (ES2015), allows elements to be added to an array or object without using concat()
or other methods.concat()
.Other Alternatives
In addition to the three approaches tested in this benchmark:
Array.prototype.push()
: Adding elements to the end of an array using push()
.Array.prototype.unshift()
: Adding elements to the beginning of an array using unshift()
.concat()
or Immutable.js's concat()
, may also be used for concatenating arrays.Keep in mind that these alternatives are not part of the standard JavaScript API and may have different performance characteristics compared to the tested options.