var t1 = [1,2,3,4,5,6,7,8,9,0];
var t2 = ['a', 'b', 'c', 'd', 'e'];
const c = [t1, t2];
const c = t1.concat(t2);
t1.push(t2);
const c= t1;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
spread | |
concat | |
push |
Test name | Executions per second |
---|---|
spread | 5072912.5 Ops/sec |
concat | 3480123.2 Ops/sec |
push | 2888457.8 Ops/sec |
Let's break down what's being tested in this JavaScript microbenchmark.
Benchmark Definition
The benchmark is designed to compare three approaches for concatenating arrays:
...
): const c = [...t1, ...t2];
concat()
method: const c = t1.concat(t2);
push()
method with spread operator (...
): t1.push(...t2); const c= t1;
Options Compared
The benchmark is comparing the performance of these three approaches:
concat()
Method: Using the concat()
method, which is a built-in array method that concatenates two or more arrays.push()
Method with Spread Operator: Using the push()
method with the spread operator (...
). This approach combines the benefits of both methods.Pros and Cons
Here's a brief summary of each approach:
concat()
Method:push()
Method with Spread Operator:push()
method call.Library Usage
None of the test cases use any external libraries.
Special JS Features or Syntax
The benchmark uses a few modern JavaScript features:
...
): This feature was introduced in ECMAScript 2015 (ES6) and allows for concise syntax when concatenating arrays.\r\n
): While not exclusive to JavaScript, template literals are used in the script preparation code to create a multi-line string.Other Alternatives
If you're looking for alternative approaches to concatenate arrays, you could consider:
Array.prototype.push.apply()
method: This method applies the spread operator's behavior to an array by passing it as an argument.Array.prototype.reduce()
method: This method can be used to concatenate arrays in a more functional programming style.Keep in mind that these alternatives may have slightly different performance characteristics and readability implications compared to the approaches tested in this benchmark.