dumb test

Benchmark created on


Setup

function getIntersectMap(arr, map) {
  const returnMap = new Map()
  for (let i = 0; i < arr.length; i++) {
    if (!map.size) {
      returnMap.set(arr[i], i)
    } else {
      if (map.has(arr[i]) && !returnMap.has(arr[i])) {
        returnMap.set(arr[i], i)
      }
    }
  }
  return returnMap
}

function intersect0() {
  const initialMap = new Map()
  const arrays = Array.from(arguments) // I am assuming, all the arguments will be arrays.
  const resultMap = arrays?.reduce(function _(acc, currentVal) {
    return getIntersectMap(currentVal, acc)
  }, initialMap)

  return Array.from(resultMap.keys())
}

function intersect1(...arrays) {
  const [smallestSet, ...otherSets] = arrays.map((arr) => new Set(arr)).sort((a, b) => a.size - b.size);

  return Array.from(smallestSet.values()).filter((val) => otherSets.every((set) => set.has(val)));
}

function intersect2(...arrays) {
  return arrays?.reduce((intersection, currentArray) => {
    const intersectionSet = new Set(intersection);
    return currentArray.filter((item) => intersectionSet.has(item));
  });
}

const arrstr__1 = [
  "Item 1",
  "Item 2",
  "Item 3",
  "Item 4",
  "Item 5",
  "Item 6",
  "Item 7",
]
const arrstr__2 = ["Item 6", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7"]
const arrstr__3 = ["Item 4", "Item 7", "Item 5", "Item 6", "Item 8", "Item 9"]
const arrstr__4 = ["Item 7", "Item 5", "Item 6", "Item 8", "Item 9", "Item 10"]
const arrstr__5 = ["Item 6", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10"]

Test runner

Ready to run.

Testing in
TestOps/sec
His
intersect1(arrstr__1, arrstr__2, arrstr__3, arrstr__4, arrstr__5)
ready
Mine
intersect2(arrstr__1, arrstr__2, arrstr__3, arrstr__4, arrstr__5)
ready
original
intersect0(arrstr__1, arrstr__2, arrstr__3, arrstr__4, arrstr__5)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.