<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 other = existing.concat(newi);
var other = [ existing, newi ];
var other = newi.forEach(i => existing.concat().push(i));
newi.forEach(i => existing.push(i));
var other = _.flatten([ existing, newi ]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.concat | |
spread operator | |
copy on push | |
directly push | |
flat |
Test name | Executions per second |
---|---|
Array.prototype.concat | 2095507.6 Ops/sec |
spread operator | 2888256.8 Ops/sec |
copy on push | 1387847.4 Ops/sec |
directly push | 1687021.2 Ops/sec |
flat | 0.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this benchmark.
Benchmark Definition
The benchmark is designed to compare four different approaches for concatenating arrays or modifying existing arrays:
Array.prototype.concat()
: Using the traditional concat()
method.[...]
): Utilizing the new ES6 spread operator to create a new array.push()
modifies an existing array by copying its elements and then pushing the new ones): This approach uses forEach()
to iterate over the new array and append it to the existing one using concat()
.newi.forEach(i => existing.push(i))
): Modifying the original array directly by iterating over the new array and pushing each element into the existing array.Pros and Cons of Each Approach
newi
). This approach creates a new array without modifying the existing one, making it more memory-efficient.concat()
and then pushing new ones): While this approach modifies an existing array, it still has the overhead of creating intermediate arrays, which might be less efficient than other methods.newi.forEach(i => existing.push(i))
): This approach directly modifies the original array by iterating over the new array and appending each element to the existing one. It's likely the most memory-efficient option.Library Usage
The benchmark uses Lodash, a popular JavaScript utility library, in its flat()
test case:
var other = _.flatten([existing, newi]);
_flatten()
is used to flatten an array of arrays into a single array. This approach is useful when you need to perform operations on nested arrays, but it may not be the most efficient way to concatenate or modify arrays.
Special JS Features/Syntax
The benchmark does not use any special JavaScript features or syntax beyond what's standard in modern browsers (ECMAScript 2015+).
Alternative Approaches
Here are some alternative approaches that could be used for this benchmark:
Array.prototype.pushAll()
: This method is available in ECMAScript 2022, which allows pushing multiple values into an array at once.existing.push(...newi);
Set
or Map
: You could create a custom function that iterates over the new array and adds each element to a Set
or Map
, which would then be converted back to an array. This approach can be more efficient than using push()
or concat()
.Keep in mind that these alternative approaches might not be as straightforward or well-documented as the original methods used in the benchmark.