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)
}
const res = _.xor(a, b)
var as = new Set(a)
var bs = new Set(b)
const res = a
.filter((row_id) => bs.has(row_id))
.concat(b.filter((row_id) => !as.has(row_id)))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash xor | |
with Sets |
Test name | Executions per second |
---|---|
lodash xor | 3463.2 Ops/sec |
with Sets | 7327.9 Ops/sec |
I'll break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is testing two different approaches to calculating the XOR (exclusive or) of two arrays, a
and b
. The first approach uses Lodash's _.xor
function, while the second approach uses JavaScript Sets.
Test Cases
There are two test cases:
_.xor
function to calculate the XOR of two arrays.Options Compared
The two approaches have different strengths and weaknesses:
Library
In the Lodash Xor test case, the _.xor
function is used. This is a popular utility library that provides various functions for functional programming tasks. In this case, it's being used to calculate the XOR of two arrays.
Special JS Feature/Syntax
None mentioned in the provided code snippets.
Benchmark Preparation Code
The preparation code generates two large arrays of random IDs using the generateId
function and assigns them to variables a
and b
.
Other Alternatives
Some alternative approaches could be:
_.xor
or JavaScript Sets, one could write a simple loop to calculate the XOR of two arrays. This would likely be slower than both of the above approaches._.xor
.