<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var existing = [ 1, 2, "hello", true, 7 ];
var newi = [1, 1, 1];
var test = existing.concat(newi);
var test = [ existing, newi ];
var test = newi.forEach(i => existing.concat().push(i));
existing.forEach(i => newi.push(i));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
copy on push | |
directly push |
Test name | Executions per second |
---|---|
Array.prototype.concat | 3642714.2 Ops/sec |
spread operator | 5745625.5 Ops/sec |
copy on push | 1481344.6 Ops/sec |
directly push | 2287795.2 Ops/sec |
Let's break down the provided benchmark and its results.
Benchmark Overview
The benchmark is designed to compare four different approaches for concatenating arrays in JavaScript:
concat()
method...
)Options Compared
The benchmark compares the performance of these four approaches:
Array.prototype.concat()
: The traditional method of concatenating arrays....
): A new syntax introduced in ES6 that allows for more concise array creation.Pros and Cons of Each Approach
Array.prototype.concat()
:...
):Library Used
The benchmark uses Lodash.js library, which is a popular utility library for JavaScript that provides various functions for tasks like array manipulation, string manipulation, and more. In this case, it's used in the "Html Preparation Code" section to include the Lodash.js file on the webpage.
Special JS Features/Syntax
None of the benchmark tests use any special JavaScript features or syntax beyond what's standard for ES6 and later versions. All test cases are written in standard JavaScript syntax.
Alternatives
If you're looking for alternative approaches, here are a few more options:
Array.prototype.reduce()
to concatenate arraysArray.prototype.slice()
to create a shallow copy of an array and then concatenating it with the new arrayKeep in mind that the best approach will depend on your specific use case, performance requirements, and personal preference.