Intersection Best Practices (v2)

Revision 2 of this benchmark created on


Setup

const SIZES = {
  small: 30,  // Well under threshold
  threshold: 60, // At threshold
  large: 120  // Well over threshold
};

// Create arrays with realistic overlap (50% shared elements)
const createArrays = (size, count = 2) => {
  const commonElements = Array.from(
    { length: Math.floor(size/2) }, 
    (_, i) => i
  );
  
  return Array.from({ length: count }, (_, arrayIndex) => [
    ...commonElements,
    ...Array.from(
      { length: Math.floor(size/2) }, 
      (_, i) => size + (arrayIndex * size) + i
    )
  ]);
};

// Small arrays (30 items each, 60 total)
const [smallArr1, smallArr2] = createArrays(SIZES.small);
// Threshold arrays (60 items each, 120 total) 
const [thresholdArr1, thresholdArr2] = createArrays(SIZES.threshold);
// Large arrays (120 items each, 240 total)
const [largeArr1, largeArr2] = createArrays(SIZES.large);

// Multi-array versions
const [multiSmall1, multiSmall2, multiSmall3] = createArrays(SIZES.small, 3);
const [multiThreshold1, multiThreshold2, multiThreshold3] = createArrays(SIZES.threshold, 3);
const [multiLarge1, multiLarge2, multiLarge3] = createArrays(SIZES.large, 3);

Test runner

Ready to run.

Testing in
TestOps/sec
Filter approach - Small arrays
[...new Set(smallArr1)].filter(x => smallArr2.includes(x));
ready
Set approach - Small arrays
const smallSet2 = new Set(smallArr2);
[...new Set(smallArr1)].filter(x => smallSet2.has(x));
ready
Filter approach - Threshold arrays
[...new Set(thresholdArr1)].filter(x => thresholdArr2.includes(x));
ready
Set approach - Threshold arrays
const threshSet2 = new Set(thresholdArr2);
[...new Set(thresholdArr1)].filter(x => threshSet2.has(x));
ready
Filter approach - Large arrays
[...new Set(largeArr1)].filter(x => largeArr2.includes(x));
ready
Set approach - Large arrays
const largeSet2 = new Set(largeArr2);
[...new Set(largeArr1)].filter(x => largeSet2.has(x));
ready
Filter approach - Small Multi
[...new Set(multiSmall1)].filter(x => 
  [multiSmall2, multiSmall3].every(arr => arr.includes(x))
);
ready
Set approach - Small Multi
const multiSmallSets = [multiSmall2, multiSmall3].map(arr => new Set(arr));
[...new Set(multiSmall1)].filter(x =>
  multiSmallSets.every(set => set.has(x))
);
ready
Filter approach - Threshold Multi
[...new Set(multiThreshold1)].filter(x => 
  [multiThreshold2, multiThreshold3].every(arr => arr.includes(x))
);
ready
Set approach - Threshold Multi
const multiThresholdSets = [multiThreshold2, multiThreshold3].map(arr => new Set(arr));
[...new Set(multiThreshold1)].filter(x =>
  multiThresholdSets.every(set => set.has(x))
);
ready
Filter approach - Large Multi
[...new Set(multiLarge1)].filter(x => 
  [multiLarge2, multiLarge3].every(arr => arr.includes(x))
);
ready
Set approach - Large Multi
const multiLargeSets = [multiLarge2, multiLarge3].map(arr => new Set(arr));
[...new Set(multiLarge1)].filter(x =>
  multiLargeSets.every(set => set.has(x))
);
ready

Revisions

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