var array1 = [ "hella", true, 7 ]
var array2 = ["hallo", false, 5];
var other = [ array1, array2 ]
var final = array1.concat(array2)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread operator | |
concat into empty array |
Test name | Executions per second |
---|---|
spread operator | 4304849.0 Ops/sec |
concat into empty array | 2724667.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition and Purpose
The provided JSON represents a benchmark that compares two approaches for concatenating arrays: the traditional concat()
method and the new ES6 spread operator (...
). The purpose of this benchmark is to determine which approach is faster in JavaScript.
Options Compared
There are two test cases:
[ ...array1, ...array2 ]
): This option uses the spread operator to concatenate array1
and array2
. It's a concise way to create a new array by copying elements from an existing array.var final = array1.concat(array2)
): This option concatenates array1
and array2
using the concat()
method, which creates a new array by copying elements from each input array.Pros and Cons
Library and Purpose (None in this case)
There are no external libraries used in this benchmark. The tests rely solely on built-in JavaScript features.
Special JS Feature or Syntax (Spread Operator)
The spread operator ([ ...array1, ...array2 ]
) is a new feature introduced in ECMAScript 2015 (ES6). It allows you to create a new array by copying elements from an existing array. This syntax was not available in older versions of JavaScript.
Other Alternatives
If you need to concatenate arrays, there are other approaches:
Array.prototype.push()
: array1.push(...array2);
Array.from()
method: Array.from(array1).concat(Array.from(array2))
Keep in mind that these alternatives may have different performance characteristics compared to the spread operator and the concat() method.
I hope this explanation helps you understand the benchmark and its options!