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, _.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 |
---|---|
current xor implementation | |
alternative with Sets |
Test name | Executions per second |
---|---|
current xor implementation | 1053.6 Ops/sec |
alternative with Sets | 6459.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is searching for faster solution for _.xor(a, _.xor(a,b))
, which is a common operation in JavaScript that computes the bitwise XOR (exclusive or) of two arrays. The goal is to find a more efficient implementation than the current one used by Lodash.
Options Compared
Two options are compared:
xor
function.Pros and Cons of Each Approach
Current Implementation
Pros:
Cons:
xor
function may be slower than other implementations due to its complexity.Alternative with Sets
Pros:
Cons:
Library Used (Set)
The Set
data structure is used in the alternative implementation. Sets are a native JavaScript data structure that stores unique values, allowing for efficient membership testing and iteration. In this case, creating two sets (as
and bs
) allows for a fast lookup of elements in both arrays.
Special JS Feature/Syntax
There doesn't appear to be any special JavaScript features or syntax used in the benchmark.
Other Alternatives
Other alternatives to improve performance might include:
map()
, filter()
, and reduce()
might be faster than using a library function.These alternatives would require more extensive investigation and testing to determine their feasibility and effectiveness.