<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js'></script>
var arr1 = new Array(1000).fill(0);
var arr2 = new Array(1000).fill(1);
[arr1, arr2]
_.union(arr1, arr2)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Spread Operator | |
Lodash union |
Test name | Executions per second |
---|---|
Spread Operator | 187966.1 Ops/sec |
Lodash union | 40829.1 Ops/sec |
I'd be happy to explain what's being tested in the provided benchmark.
What is being tested?
The benchmark compares two approaches to create a new array by combining two existing arrays: using the spread operator ([...arr1, ...arr2]
) and using Lodash's union
function (_.union(arr1, arr2)
).
Options compared
Two options are being compared:
...
operator to create a new array by spreading the elements of two arrays.union
function to create a new array containing all unique elements from two arrays.Pros and Cons
Spread Operator:
Pros:
Cons:
Lodash Union:
Pros:
Cons:
Other considerations
Both approaches assume that the input arrays arr1
and arr2
are already created and contain elements to be combined. The benchmark uses JavaScript arrays as the input data structures.
Library: Lodash
The union
function from Lodash is used in the benchmark, which provides a convenient way to combine arrays while ignoring duplicates.
Special JS feature or syntax (not applicable)
There are no special JavaScript features or syntaxes being tested in this benchmark.
Other alternatives
If you were to implement your own array union logic, you could consider using:
Keep in mind that implementing these alternatives would likely be slower than using the Lodash union
function or the spread operator, especially for large datasets.