<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
array1 = [1, 2, 3];
array2 = [3, 4, 5];
_.union(array1, array2);
[new Set([array1, array2])];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.union | |
Native JavaScript |
Test name | Executions per second |
---|---|
lodash.union | 3791506.2 Ops/sec |
Native JavaScript | 3050572.2 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and the pros and cons of each approach.
Benchmark Overview
The benchmark compares the performance of two approaches:
union
functionArray.prototype.concat()
method combined with new Set()
What is being tested?
Options compared
union
function: This approach uses a JavaScript library (Lodash) to perform the union operation.Array.prototype.concat()
and new Set()
).Considerations
When choosing between these approaches, consider the following:
union
function could be preferred.union
function could suffice.Library (Lodash) and its purpose
Lodash is a popular JavaScript utility library that provides a comprehensive set of functions for tasks beyond basic array and object manipulation. In this case, the union
function is used to merge multiple arrays into a single array with unique values.
The library adds value by:
However, it also introduces an external dependency that may impact execution speed.
Special JS feature or syntax
There is no special JavaScript feature or syntax used in this benchmark. Both approaches utilize standard JavaScript functions and syntax (e.g., Array.prototype.concat()
and new Set()
).
Other alternatives
If you're looking for alternative implementations, consider:
p-union
or lodash.merge
) that might offer improved performance or features.