var array1 = Array.from({length: 40}, () => Math.floor(Math.random() * 140));
var array2 = Array.from({length: 20}, () => Math.floor(Math.random() * 140));
var setFromArray1 = new Set(array1)
var setFromArray2 = new Set(array2)
const setDiff = setFromArray1.difference(setFromArray2)
const arrayFilter = array1.filter(value => array2.indexOf(value))
const arrayFilterWithSetHas = array1.filter(value => setFromArray2.has(value))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set.difference | |
Array.filter | |
Array.filter with Set.has |
Test name | Executions per second |
---|---|
Set.difference | 0.0 Ops/sec |
Array.filter | 957967.7 Ops/sec |
Array.filter with Set.has | 2532799.8 Ops/sec |
The benchmark being tested is comparing the performance of three different approaches to find the difference between two sets of values represented by arrays in JavaScript. Below is a detailed explanation of the options being compared, their pros and cons, and the libraries used, if any.
Set.difference:
const setDiff = setFromArray1.difference(setFromArray2);
.difference
on the Set
object that is meant to calculate the difference between setFromArray1
and setFromArray2
.0.0
, indicating that the method was either not implemented or incorrectly called since the native Set
object in JavaScript does not have a difference
method natively.Array.filter:
const arrayFilter = array1.filter(value => array2.indexOf(value));
filter
method on the first array (array1
) to return a new array containing only those elements that exist in array2
(using indexOf
).957967.6875
executions per second, which demonstrates that while it works, this approach could be inefficient for larger datasets.indexOf
results in a time complexity of O(n*m), worsening performance with larger arrays, as it checks each element of array1
against all elements in array2
.Array.filter with Set.has:
const arrayFilterWithSetHas = array1.filter(value => setFromArray2.has(value));
array1
and checks if each value exists in setFromArray2
using the .has
method of the Set
. This provides a more efficient check due to the underlying data structure's O(1) average time complexity for lookups.2532799.75
executions per second, showing it's significantly more efficient than the previous two approaches.Set
for lookups dramatically increases the efficiency of the operation, making it the optimal choice for this use case.In summary, the benchmark assesses three methods for computing the difference/matching values between two arrays. The first method attempts to leverage a non-existent Set.difference
method, which resulted in an ineffective benchmark. The second method using Array.filter
with indexOf
is straightforward but inefficient for larger datasets due to its O(n*m) time complexity. The best performance came from using Array.filter
along with Set.has
, which provides a clear performance advantage.
Alternative approaches might include:
Set
for lookups, balancing performance and clarity.These alternatives would depend on project requirements, including complexity, readability, and performance optimizations.