mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Fix perf regression in _.intersection.
This commit is contained in:
23
lodash.js
23
lodash.js
@@ -2737,46 +2737,43 @@
|
|||||||
* _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
|
* _.intersection([1, 2, 3], [5, 2, 1, 4], [2, 1]);
|
||||||
* // => [1, 2]
|
* // => [1, 2]
|
||||||
*/
|
*/
|
||||||
function intersection(array) {
|
function intersection() {
|
||||||
if (!array) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
var args = [],
|
var args = [],
|
||||||
argsIndex = -1,
|
argsIndex = -1,
|
||||||
argsLength = arguments.length,
|
argsLength = arguments.length,
|
||||||
caches = [],
|
caches = [],
|
||||||
indexOf = getIndexOf(),
|
indexOf = getIndexOf(),
|
||||||
prereq = createCache && indexOf === baseIndexOf,
|
prereq = createCache && indexOf === baseIndexOf;
|
||||||
seen = [];
|
|
||||||
|
|
||||||
while (++argsIndex < argsLength) {
|
while (++argsIndex < argsLength) {
|
||||||
var value = arguments[argsIndex];
|
var value = arguments[argsIndex];
|
||||||
if (isArray(value) || isArguments(value)) {
|
if (isArray(value) || isArguments(value)) {
|
||||||
args.push(value);
|
args.push(value);
|
||||||
caches.push(prereq && value.length >= 120 &&
|
caches.push(prereq && value.length >= 120 &&
|
||||||
createCache(argsIndex ? value : seen));
|
createCache(argsIndex && value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
argsLength = args.length;
|
argsLength = args.length;
|
||||||
var array = args[0],
|
var array = args[0],
|
||||||
index = -1,
|
index = -1,
|
||||||
length = array ? array.length : 0,
|
length = array ? array.length : 0,
|
||||||
result = [];
|
result = [],
|
||||||
|
seen = caches[0];
|
||||||
|
|
||||||
outer:
|
outer:
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
var cache = caches[0];
|
|
||||||
value = array[index];
|
value = array[index];
|
||||||
|
if ((seen ? cacheIndexOf(seen, value) : indexOf(result, value)) < 0) {
|
||||||
if ((cache ? cacheIndexOf(cache, value) : indexOf(seen, value)) < 0) {
|
|
||||||
argsIndex = argsLength;
|
argsIndex = argsLength;
|
||||||
(cache || seen).push(value);
|
|
||||||
while (--argsIndex) {
|
while (--argsIndex) {
|
||||||
cache = caches[argsIndex];
|
var cache = caches[argsIndex];
|
||||||
if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) {
|
if ((cache ? cacheIndexOf(cache, value) : indexOf(args[argsIndex], value)) < 0) {
|
||||||
continue outer;
|
continue outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (seen) {
|
||||||
|
seen.push(value);
|
||||||
|
}
|
||||||
result.push(value);
|
result.push(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user