<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
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());
}
function getRandom() {
const minimum = 1;
const maximum = 1000;
var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
return randomnumber;
}
const results = new Set(_.intersection(arr1, arr2, arr3, arr4));
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);
const results = new Set(
[arr1, arr2, arr3, arr4]
.sort((a, b) => b.length - a.length)
.reduce((a, b) => a.filter(aValue => b.includes(aValue)))
);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash _.intersection() | |
Custom optimized function 1 (Best) | |
Custom optimized function 2 |
Test name | Executions per second |
---|---|
Lodash _.intersection() | 8922.4 Ops/sec |
Custom optimized function 1 (Best) | 10882.7 Ops/sec |
Custom optimized function 2 | 4526.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark measures the performance of three approaches to find the intersection of multiple arrays:
_.intersection()
methodWhat is being tested?
arr1
, arr2
, arr3
, and arr4
) containing random integers between 1 and 1000._intersection()
methodOptions Compared
The benchmark compares two custom optimized functions, which are essentially implementations of set intersection. The main differences between these two functions are:
Set
reduce()
method with a filter function to find the intersectionPros and Cons
Other Considerations
_intersection()
method is a convenient and widely-used solution, but its performance may vary depending on the specific input data.Set
data structure, which provides efficient set operations.Library Used: Lodash
Lodash is a popular JavaScript library that provides a wide range of utility functions. In this case, the _intersection()
method is used to find the intersection of multiple arrays. Other useful methods from Lodash include _.uniq()
, _.uniqWith()
, and _.difference()
, which perform similar set operations.
Special JS Feature: None
There are no special JavaScript features or syntaxes being tested in this benchmark.
Alternatives
Other alternatives for finding the intersection of multiple arrays include:
Array.prototype.filter()
and accumulating the results