{"ScriptPreparationCode":"var arr1 = [];\r\nvar arr2 = [];\r\nvar arr3 = [];\r\nvar arr4 = [];\r\n\r\nfor (i = 0; i \u003C 1000; i\u002B\u002B) {\r\n arr1.push(getRandom());\r\n arr2.push(getRandom());\r\n arr3.push(getRandom());\r\n arr4.push(getRandom());\r\n}\r\n\r\nfunction getRandom() {\r\n const minimum = 1;\r\n const maximum = 1000;\r\n var randomnumber = Math.floor(Math.random() * (maximum - minimum \u002B 1)) \u002B minimum;\r\n return randomnumber;\r\n}","TestCases":[{"Name":"Lodash _.intersection()","Code":"const results = new Set(_.intersection(arr1, arr2, arr3, arr4));","IsDeferred":false},{"Name":"Custom optimized function 1 (Best)","Code":"function intersectMultipleArrays(...arrays) {\r\n if (arrays.length === 0) return new Set();\r\n\r\n // Sort arrays by length to optimize operations\r\n arrays.sort((a, b) =\u003E a.length - b.length);\r\n\r\n // Start with the smallest array as the base intersection set\r\n let intersection = new Set(arrays[0]);\r\n\r\n // Iterate over the remaining arrays and filter the intersection\r\n for (let i = 1; i \u003C arrays.length; i\u002B\u002B) {\r\n intersection = new Set(arrays[i].filter(item =\u003E intersection.has(item)));\r\n \r\n // Early exit if intersection is empty\r\n if (intersection.size === 0) return new Set();\r\n }\r\n\r\n // Return the final Set\r\n return intersection;\r\n}\r\n\r\nconst results = intersectMultipleArrays(arr1, arr2, arr3, arr4);","IsDeferred":false},{"Name":"Custom optimized function 2","Code":"const results = new Set(\r\n [arr1, arr2, arr3, arr4]\r\n .sort((a, b) =\u003E b.length - a.length)\r\n .reduce((a, b) =\u003E a.filter(aValue =\u003E b.includes(aValue)))\r\n);","IsDeferred":false}]}