Script Preparation code:
x
 
var empty = { id: '', number: 10000 };
var array1 = [empty];
var array2 = [empty];
var entities = { '': empty };
var id = '';
for (let i = 0; i < 10000; 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 < 100; i++) {
  array1.push(entities[getRandomInt(i, 10000)]);
}
for (let i = 0; i < 100; i++) {
  array2.push(entities[getRandomInt(i, 10000)]);
}
Tests:
  • Array.includes

     
    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)
  • Map.has

     
    var lMap = new Map(array1.map((item) => [item, true]));
    var rMap = new Map(array2.map((item) => [item, true]));
    var unionMap = new Map([...lMap.entries(), ...rMap.entries()]);
    var result = { rhsOnly: [], lhsOnly: [], union: [] };
    for (var [item] of unionMap) {
      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)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Array.includes
    Map.has

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36
Chrome 98 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
Array.includes 16672.3 Ops/sec
Map.has 21708.9 Ops/sec