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)
function systemicDifference(arr1, arr2){
const uniqueValues = new Set([arr1, arr2]);
return Array.from(uniqueValues).filter((value) => !arr1.includes(value) || !arr2.includes(value));
}
const res = systemicDifference(a, b)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash xor | |
with xor function |
Test name | Executions per second |
---|---|
lodash xor | 3153.6 Ops/sec |
with xor function | 182.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and other considerations.
Benchmark Definition
The benchmark is defined by two separate JSON objects:
dec2hex
and generateId
. The generateId
function generates a random ID in hexadecimal format using the Web Crypto API.dec2hex
: converts a decimal number to a hexadecimal string.generateId
: generates a random ID by generating an array of 8-bit values ( Uint8Array ) using the Web Crypto API, then converting each value to a hexadecimal string using dec2hex
, and joining them together.The script preparation code creates two arrays: a
and b
, both with 1000 elements. Each element in the arrays is generated by calling generateId(16)
, which produces a random ID for each element.
null
) and does not appear to be used in this benchmark.Individual Test Cases
The benchmark consists of two test cases:
lodash xor
: This test case only includes the code const res = _.xor(a, b)
. It appears that _
refers to the Lodash library.
_.xor
likely performs an element-wise XOR operation between two arrays.with xor function
: This test case includes additional code: function systemicDifference(arr1, arr2){...}
.
xor
function and a custom implementation.Other Considerations
When evaluating performance, we need to consider factors like:
a
and b
align in memory?Pros and Cons
Lodash's _xor
function
Pros:
Cons:
Custom systemicDifference
implementation
Pros:
Cons:
Benchmark Result Interpretation
The benchmark result shows the performance of each test case across multiple executions. The results indicate that Lodash's _xor
function is significantly faster than the custom implementation in this specific scenario.
Keep in mind that performance results can vary greatly depending on factors like hardware, software configurations, and system load.
Alternatives
If you were to rewrite the benchmark or create an alternative implementation, consider using:
Array.prototype.reduce
instead of Lodash's _xor
).By understanding the benchmark's setup, options, and considerations, you can create more effective comparisons between different implementations.