<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
var a = [1, 2, 3, 4, 5]
var b = [3, 4, 5, 6, 7]
var c = _.union(a, b)
var c = [new Set(a, b)]
var c = new Set(a, b)
var d = Array.from(c)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.union vs set for unique primitives | |
Set() | |
Set() convert back to array |
Test name | Executions per second |
---|---|
_.union vs set for unique primitives | 2677196.5 Ops/sec |
Set() | 3480754.0 Ops/sec |
Set() convert back to array | 2687544.0 Ops/sec |
Let's dive into the benchmark.
What is being tested?
The benchmark is comparing three approaches to union (combine) two arrays of primitive values:
union()
functionSet
data structure and converting it back to an array using Array.from()
Set
data structure without converting it back to an arrayOptions compared
The benchmark is comparing two main approaches:
union()
function: This approach uses a third-party library (Lodash) to perform the union operation.Set
with conversion: This approach uses the native JavaScript Set
data structure to store unique values and then converts it back to an array using Array.from()
. This approach is built-in to JavaScript.Pros and Cons
Lodash's union()
function:
Pros:
Cons:
Native JavaScript Set
with conversion:
Pros:
Cons:
Set
data structure and its methods.Set
object.Other considerations
union()
function might introduce additional variability in the results, as it depends on the specific version of Lodash used.Set
to an array using Array.from()
might introduce additional overhead due to the extra method call.Library and its purpose
In this benchmark, the library is Lodash, a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. The union()
function in particular is used to combine two arrays into one, removing duplicates.
Special JS feature or syntax
There isn't any special JS feature or syntax being tested in this benchmark. It's purely about comparing different approaches to unioning arrays of primitive values.