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.lenght !== b.lengh && a.all((x) => bs.has(x))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
set diff |
Test name | Executions per second |
---|---|
lodash | 8525.6 Ops/sec |
set diff | 11402861.0 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared options, pros/cons of each approach, and other considerations.
Benchmark Definition
The provided JSON defines two microbenchmarks: "Array diff" and two test cases for libraries Lodash and Set.
What is tested?
a
and b
with 1000 elements each, generates random IDs using the generateId
function (explained later), and checks if there are any differences between the two arrays._.xor
function, which returns an array of elements that are present in a
but not in b
, or vice versa. The test case measures the performance of the lenght
property access (a typo?) on the result.b
and checks if any elements in a
are present in the set.Options Compared
every
, some
) vs. custom implementations._.xor
function is compared to a custom implementation that uses a Set.Pros/Cons and Considerations
_.xor
function:Libraries and Special Features
_.xor
function is used.Other Alternatives
every
, some
, or filter
could be used.The provided benchmark allows for comparing different approaches to array comparison and set operations, enabling users to choose the most efficient method for their specific use case.