const response = [ { 'hundo': 100 }, { 'nine': 9 }, {'nested': { 'twos': 22 }} ];
const state = [ { 'ten': 10 }, { 'six': 6 }, {'nested': { 'five': 5 }} ].concat(response);
const response = [ { 'hundo': 100 }, { 'nine': 9 }, {'nested': { 'twos': 22 }} ];
const state = [ response, { 'ten': 10 }, { 'six': 6 }, {'nested': { 'five': 5 }} ];
const prevState = [ { 'ten': 10 }, { 'six': 6 }, {'nested': { 'five': 5 }} ];
const newItems = [ { 'hundo': 100 }, { 'nine': 9 }, {'nested': { 'twos': 22 }} ];
const state = [ prevState, newItems ];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator (spread at head) | |
spread operator x2 (head and tail) |
Test name | Executions per second |
---|---|
Array.prototype.concat | 6621629.0 Ops/sec |
spread operator (spread at head) | 10978518.0 Ops/sec |
spread operator x2 (head and tail) | 9088666.0 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Overview
The benchmark compares two approaches for merging two object arrays: Array.prototype.concat
and the new ES6 spread operator (...
). The test aims to determine which approach is faster.
Options compared
There are three test cases:
...
) to create a new array by copying elements from one or more source arrays. In this test case, the spread operator is used with response
as the first argument and state
as the second argument.prevState
and newItems
) at the head and once to merge the resulting array with another array (state
).Pros and cons
Library and purpose
None mentioned in the benchmark definition. However, both Array.prototype.concat
and the spread operator use native JavaScript functionality.
Special JS feature or syntax
The spread operator (...
) is a modern JavaScript feature introduced in ES6 (ECMAScript 2015). It allows you to create new arrays by copying elements from existing arrays.
Other alternatives
Before the spread operator, developers often used Array.prototype.push
and Array.prototype.concat
, which also merge arrays. However, these approaches can be less efficient due to the overhead of modifying original arrays.
The benchmark results show that the spread operator (spread at head) is currently the fastest approach on this platform, followed by the spread operator x2 (head and tail) variant, and then Array.prototype.concat.