<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var arr1 = [];
for(let i = 0; i < 400; i++) {
arr1.push('' + i);
}
var arr2 = [];
for(let i = 49; i >= 0; i--) {
arr2.push('' + i);
}
const notInArr1 = _.difference(arr2, arr1)
const notInArr2 = _.difference(arr1, arr2)
const arr1Set = new Set();
const arr2Set = new Set();
arr1.forEach(value => arr1Set.add(value));
arr2.forEach(value => arr2Set.add(value));
const notInArr1 = arr2.filter(value => !arr1Set.has(value));
const notInArr2 = arr1.filter(value => !arr2Set.has(value));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Set & Filter |
Test name | Executions per second |
---|---|
Lodash | 25509.0 Ops/sec |
Set & Filter | 114994.1 Ops/sec |
Let's break down the provided benchmarking JSON and explain what's being tested.
Benchmark Definition
The benchmark measures the performance difference between two approaches:
_.difference
function.filter()
method) to achieve the same result.Options compared
In this benchmark, two options are being compared:
_.difference
functionPros and Cons of each approach:
Library: Lodash
Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object transformation, and functional programming. It's widely used in web development due to its extensive feature set and well-maintained codebase. In this benchmark, Lodash is used to provide a concise way to find differences between arrays.
Special JS feature: None
There are no special JavaScript features or syntaxes being tested in this benchmark.
Other alternatives
If the Set & Filter approach isn't suitable for your use case, other alternatives could include:
Map
or WeakSet
)