dumb test (v2)

Revision 2 of this 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 intersect_isamrish() {
  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 intersect_blackholesinthesky(...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 intersect_jeremrx(...arrays) {
  return arrays?.reduce((intersection, currentArray) => {
    const intersectionSet = new Set(intersection);
    return currentArray.filter((item) => intersectionSet.has(item));
  });
}


function intersect_jeremrx_2(firstArray, ...otherArrays) {
  const intersection = new Set(firstArray);
  otherArrays.forEach((array) =>
    array.forEach((item) => intersection.has(item) || intersection.delete(item))
  );
  return [...intersection];
}


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
isamrish
intersect_isamrish(arrstr__1, arrstr__2, arrstr__3, arrstr__4, arrstr__5)
ready
blackholesinthesky
intersect_blackholesinthesky(arrstr__1, arrstr__2, arrstr__3, arrstr__4, arrstr__5)
ready
jeremrx
intersect_jeremrx(arrstr__1, arrstr__2, arrstr__3, arrstr__4, arrstr__5)
ready
jeremrx reworked
intersect_jeremrx_2(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.