Optimize _.intersection to stop search once it matches the size of its smallest array.

This commit is contained in:
John-David Dalton
2016-02-27 18:35:18 -08:00
parent 8278d38a8e
commit e842bec025

View File

@@ -2769,6 +2769,7 @@
othLength = arrays.length,
othIndex = othLength,
caches = Array(othLength),
maxLength = Infinity,
result = [];
while (othIndex--) {
@@ -2776,18 +2777,20 @@
if (othIndex && iteratee) {
array = arrayMap(array, baseUnary(iteratee));
}
caches[othIndex] = !comparator && (iteratee || array.length >= 120)
var length = array.length;
maxLength = length < maxLength ? length : maxLength;
caches[othIndex] = !comparator && (iteratee || length >= 120)
? new SetCache(othIndex && array)
: undefined;
}
array = arrays[0];
length = array.length;
var index = -1,
length = array.length,
seen = caches[0];
outer:
while (++index < length) {
while (++index < length && result.length < maxLength) {
var value = array[index],
computed = iteratee ? iteratee(value) : value;
@@ -2795,7 +2798,7 @@
? cacheHas(seen, computed)
: includes(result, computed, comparator)
)) {
var othIndex = othLength;
othIndex = othLength;
while (--othIndex) {
var cache = caches[othIndex];
if (!(cache