var empty = { id: '', number: 10000 };
var array1 = [empty];
var array2 = [empty];
var entities = { '': empty };
var id = '';
for (let i = 0; i < 1500; i++) {
id = String(i);
entities[id] = { id, number: i }
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
for (let i = 0; i < 500; i++) {
array1.push(entities[getRandomInt(i, 1500)]);
}
for (let i = 0; i < 500; i++) {
array2.push(entities[getRandomInt(i, 1500)]);
}
var data = Array.from(new Set([array1, array2])).reduce(
(acc, item) => {
if (array1.includes(item)) {
if (array2.includes(item)) {
acc.union.push(item);
} else {
acc.lhsOnly.push(item);
}
} else {
acc.rhsOnly.push(item);
}
return acc;
},
{ rhsOnly: [], lhsOnly: [], union: [] }
);
console.log(data)
var unionArray = Array.from(new Set([array1, array2]));
var data = { rhsOnly: [], lhsOnly: [], union: [] }
for (var item of unionArray) {
if (array1.includes(item)) {
if (array2.includes(item)) {
data.union.push(item);
} else {
data.lhsOnly.push(item);
}
} else {
data.rhsOnly.push(item);
}
}
console.log(data)
var lMap = new Map(array1.map((item) => [item, true]));
var rMap = new Map(array2.map((item) => [item, true]));
var unionSet = new Set([lMap.values(), rMap.values()]);
var result = { rhsOnly: [], lhsOnly: [], union: [] };
for (var item of unionSet) {
if (lMap.has(item)) {
if (rMap.has(item)) {
result.union.push(item);
} else {
result.lhsOnly.push(item);
}
} else {
result.rhsOnly.push(item);
}
}
console.log(result)
var lSet = new Set(array1);
var rSet = new Set(array2);
var unionSet = new Set([lSet.values(), rSet.values()]);
var result = { rhsOnly: [], lhsOnly: [], union: [] };
for (var item of unionSet) {
if (lSet.has(item)) {
if (rSet.has(item)) {
result.union.push(item);
} else {
result.lhsOnly.push(item);
}
} else {
result.rhsOnly.push(item);
}
}
console.log(result)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.includes - reduce | |
Array.includes - for of | |
Map.has | |
Set.has |
Test name | Executions per second |
---|---|
Array.includes - reduce | 2707.4 Ops/sec |
Array.includes - for of | 2696.5 Ops/sec |
Map.has | 11667.2 Ops/sec |
Set.has | 8881.0 Ops/sec |
Let's break down what this benchmark is testing and the pros/cons of each approach.
Benchmark Description
The benchmark tests how efficiently different data structures can be used to find the union (common elements) between two arrays, array1
and array2
. The test cases cover three approaches: using Array.includes
, Map.has
, and Set.has
.
Test Cases
Array.reduce
with Array.includes
to find the union.array1
and array2
, checks if an element is in both arrays using includes
, and categorizes it as either "lhsOnly", "rhsOnly", or "union".for...of
loop with Array.includes
to find the union.Map
objects (lMap
and rMap
) with has
to find the union.array1
and array2
, finds the union of their values, and then categorizes each item as either "lhsOnly", "rhsOnly", or "union".Set
objects (lSet
and rSet
) with has
to find the union.Latest Benchmark Results
The latest results show:
Test Name | Executions Per Second |
---|---|
Map.has | 11,667.24 |
Set.has | 8,880.97 |
Array.includes - reduce | 2,707.35 |
Array.includes - for of | 2,696.55 |
Conclusion
The Map
and Set
approaches are the clear winners in terms of performance, with Map.has
being slightly faster than Set.has
. The Array.includes
methods, while simple to understand, have slower performance due to their iterative nature. If you need to find unions between two arrays frequently, using Map
or Set
would be a good choice for optimized performance.