<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var a = [ 'a', 'b', 'c', 'b', 'd', 'a', 'e', 'f' ];
var b = [ 'b', 'd', 'a', 'e', 'f' ];
var c = _.union(a);
var a = [ 'a', 'b', 'c', 'b', 'd', 'a', 'e', 'f' ];
var b = [ 'b', 'd', 'a', 'e', 'f' ];
var c = _.uniq(a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash union | |
lodash uniq spread |
Test name | Executions per second |
---|---|
lodash union | 4441646.5 Ops/sec |
lodash uniq spread | 6909376.0 Ops/sec |
Let's break down the provided JSON data and explain what is being tested.
Benchmark Overview
The benchmark measures the performance of three different approaches to merge two arrays in JavaScript: using _.union
from Lodash, object.assign()
, and creating a new array with unique elements using _.uniq()
followed by spreading the original array (_.spread()
).
Options Compared
_
object's union()
function to merge two arrays into a single array without duplicates.object.assign()
method to create a new object with the merged arrays as properties._.uniq()
function to remove duplicates from one of the arrays, then spreads the original array using the spread operator (...
) to merge it with the other array.Pros and Cons
Lodash Libraries
...
) is used to merge the original array with the unique array.Special JavaScript Features/Syntax
None mentioned in the provided benchmark JSON data.
Other Alternatives
If you're interested in exploring other approaches, here are some additional options:
push()
to add elements to the end of one array and then use indexOf()
to check if an element exists in that array, effectively removing duplicates.Set.prototype.union()
, and then convert the result back to an array.Keep in mind that these alternatives might have different performance characteristics compared to the approaches mentioned in the benchmark.