HTML Preparation code:
x
 
1
2
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
 
var arr1 = [];
var arr2 = [];
var arr3 = [];
var arr4 = [];
for (i = 0; i < 1000; i++) {
    arr1.push(getRandom());
    arr2.push(getRandom());
    arr3.push(getRandom());
    arr4.push(getRandom());
    arr1.sort();
    arr2.sort();
    arr3.sort();
    arr4.sort();
}
function getRandom() {
    const minimum = 1;
    const maximum = 1000;
    var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
    return randomnumber;
}
Tests:
  • Lodash _.intersection()

     
    const results = new Set(_.intersection(arr1, arr2, arr3, arr4));
  • Custom optimized function 1 (Best for arbitrary numbers)

     
    function intersectMultipleArrays(...arrays) {
      if (arrays.length === 0) return new Set();
      // Sort arrays by length to optimize operations
      arrays.sort((a, b) => a.length - b.length);
      // Start with the smallest array as the base intersection set
      let intersection = new Set(arrays[0]);
      // Iterate over the remaining arrays and filter the intersection
      for (let i = 1; i < arrays.length; i++) {
        intersection = new Set(arrays[i].filter(item => intersection.has(item)));
        
        // Early exit if intersection is empty
        if (intersection.size === 0) return new Set();
      }
      // Return the final Set
      return intersection;
    }
    const results = intersectMultipleArrays(arr1, arr2, arr3, arr4);
  • Custom optimized function 2

     
    const results = new Set(
      [arr1, arr2, arr3, arr4]
      .sort((a, b) => b.length - a.length)
      .reduce((a, b) => a.filter(aValue => b.includes(aValue)))
    );
  • Custom optimized function 3 (Best for reasonable max)

     
    function intersectMultipleArraysRevised(...arrays) {
      let maximum = -1;
      for (let i = 0; i < arrays.length; i++) {
        for (let j = 0; j < arrays[i].length; j++) {
            const num = arrays[i][j];
            if (num > maximum) {
                maximum = num;
            }
        }
      }
      const count = new Array(maximum + 1).fill(0);
      for (let i = 0; i < arrays.length; i++) {
        for (let j = 0; j < arrays[i].length; j++) {
            const num = arrays[i][j];
            count[num]++;
        }
      }
      const result = new Set();
      const arrLen = arrays.length;
      for (let i = 0; i <= maximum; i++) {
        if (count[i] === arrLen) {
            result.add(i);
        }
      }
      return result;
    }
    const results = intersectMultipleArraysRevised(arr1, arr2, arr3, arr4);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Lodash _.intersection()
    Custom optimized function 1 (Best for arbitrary numbers)
    Custom optimized function 2
    Custom optimized function 3 (Best for reasonable max)

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 4 months ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
Lodash _.intersection() 11573.1 Ops/sec
Custom optimized function 1 (Best for arbitrary numbers) 15326.1 Ops/sec
Custom optimized function 2 6522.7 Ops/sec
Custom optimized function 3 (Best for reasonable max) 91182.9 Ops/sec