dumb test (v9)

Revision 9 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.sort((a, b) => a.length - b.length);

  return Array.from((new Set(smallestSet)).values()).filter((val) => otherSets.every((set) => set.includes(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);
  return Array.from(intersection.values()).filter((val) => { return otherArrays.every((otherArr) => otherArr.includes(val)) })
}


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 = []
const arrstr__5 = ["Item 6", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10", "Item 7", "Item 5", "Item 8", "Item 9", "Item 10"]

for(let i = 0; i < 100; i++) {
	arrstr__4.push(`Item ${i}`)
}

Test runner

Ready to run.

Testing in
TestOps/sec
jeremrx terror - dup
intersect_jeremrx(arrstr__5, arrstr__5, arrstr__5, arrstr__5, arrstr__5)
ready
blackholesinthesky terror - dup
intersect_blackholesinthesky(arrstr__5, arrstr__5, arrstr__5, arrstr__5, arrstr__5)
ready
isamrish terror - dup
intersect_isamrish(arrstr__5, arrstr__5, arrstr__5, arrstr__5, arrstr__5)
ready

Revisions

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