<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 = _.difference(a, b)
const set1 = new Set(a);
const set2 = new Set(b);
const unionSet = [a, b];
const uniqueNumbers = unionSet.filter(
(num) => !(set1.has(num) && set2.has(num))
);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Set |
Test name | Executions per second |
---|---|
Lodash | 7676334.0 Ops/sec |
Set | 4519926.5 Ops/sec |
Let's break down the provided benchmark definition and explain what is being tested.
Benchmark Definition
The benchmark tests two approaches to calculate the set difference between two arrays: using the Lodash
library and using the native Set
data structure in JavaScript.
Options Compared
The options being compared are:
difference()
function from the Lodash library is used to calculate the set difference.Set
object is created for each array, and then the union of both sets is calculated using the spread operator ([...a, ...b]
). Finally, an array filter is applied to remove numbers that are present in both sets.Pros and Cons
Set
include:However, the cons of using Lodash for this specific benchmark are:
Set
might be lost due to the indirection through Lodash.On the other hand, the pros of using native Set
include:
The cons of using native Set
are:
Library Used
In this benchmark, the Lodash library is used in the following way:
var c = _.difference(a, b);
The purpose of the Lodash
library is to provide a convenient way to perform common operations like set difference, union, and intersection. It allows developers to write more concise and readable code without having to implement these operations from scratch.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. However, it's worth noting that the use of const
and arrow functions ((num) => !...)
) is modern JavaScript syntax, but not necessary for understanding the underlying logic.