<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
function dec2hex (dec) {
return dec < 10
? '0' + String(dec)
: dec.toString(16)
}
function generateId (len) {
var arr = new Uint8Array((len || 40) / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, dec2hex).join('')
}
var a = []
var b = []
for( let i = 0; i < 1000; i++)
{
a[i] = generateId(16)
b[i] = generateId(16)
}
var bs = new Set(b)
const res = _.xor(a, b).lenght === 0;
const res = a.length === b.length && a.every((x) => bs.has(x))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
set diff |
Test name | Executions per second |
---|---|
lodash | 1903.1 Ops/sec |
set diff | 2217234.8 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark is testing two different approaches to find the difference between two arrays, a
and b
.
xor
function from Lodash, which returns an array containing elements that are present in either of the input arrays but not both.Options Compared
We have two options being compared:
xor
function, which is a built-in implementation for finding differences between arrays.Set
data structure to find elements that are present in one array but not the other.Pros and Cons
Library: Set
The Set
data structure is a built-in JavaScript object that allows you to store unique values. In this benchmark, it's used to find elements that are present in one array but not the other by checking if each element is already present in the set.
Special JS Feature/Syntax: None
There are no special JavaScript features or syntax being used in this benchmark.
Other Alternatives
If you're looking for alternative implementations of array difference algorithms, here are a few options:
splice
to remove elements from one array and add them to another.slice
to create a new array with only the desired elements.filter
to create a new array with elements that meet a certain condition.However, these alternatives may not be as efficient or well-tested as Lodash's implementation or the set difference approach used in this benchmark.
In summary, the benchmark is testing two different approaches to find the difference between two arrays: using Lodash's xor
function and a simple set difference calculation. The pros and cons of each approach highlight the trade-offs between using an external library (Lodash) versus a lightweight, manual implementation (set difference).