mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Optimize _.intersection.
Former-commit-id: 017b20ad958a9dd7be09872456b1d26a59361c5a
This commit is contained in:
26
lodash.js
26
lodash.js
@@ -2877,8 +2877,8 @@
|
|||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Arrays
|
* @category Arrays
|
||||||
* @param {Array} [array1, array2, ...] Arrays to process.
|
* @param {Array} [array1, array2, ...] Arrays to process.
|
||||||
* @returns {Array} Returns a new array of unique elements, in order, that are
|
* @returns {Array} Returns a new array of unique elements that are present
|
||||||
* present in **all** of the arrays.
|
* in **all** of the arrays.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
* _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
|
||||||
@@ -2887,18 +2887,28 @@
|
|||||||
function intersection(array) {
|
function intersection(array) {
|
||||||
var args = arguments,
|
var args = arguments,
|
||||||
argsLength = args.length,
|
argsLength = args.length,
|
||||||
|
cache = { '0': {} },
|
||||||
index = -1,
|
index = -1,
|
||||||
length = array ? array.length : 0,
|
length = array ? array.length : 0,
|
||||||
cache = {},
|
isLarge = length >= 100,
|
||||||
result = [];
|
result = [],
|
||||||
|
seen = result;
|
||||||
|
|
||||||
outer:
|
outer:
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
var value = array[index];
|
var value = array[index];
|
||||||
if (indexOf(result, value) < 0) {
|
if (isLarge) {
|
||||||
|
var inited = hasOwnProperty.call(cache[0], value + '')
|
||||||
|
? !(seen = cache[0][value])
|
||||||
|
: (seen = cache[0][value] = []);
|
||||||
|
}
|
||||||
|
if (inited || indexOf(seen, value) < 0) {
|
||||||
|
if (isLarge) {
|
||||||
|
seen.push(value);
|
||||||
|
}
|
||||||
var argsIndex = argsLength;
|
var argsIndex = argsLength;
|
||||||
while (--argsIndex) {
|
while (--argsIndex) {
|
||||||
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex])))(value)) {
|
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex], 0, 100)))(value)) {
|
||||||
continue outer;
|
continue outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3203,11 +3213,9 @@
|
|||||||
computed = callback ? callback(value, index, array) : value;
|
computed = callback ? callback(value, index, array) : value;
|
||||||
|
|
||||||
if (isLarge) {
|
if (isLarge) {
|
||||||
// manually coerce `computed` to a string because `hasOwnProperty`, in
|
|
||||||
// some older versions of Firefox, coerces objects incorrectly
|
|
||||||
var inited = hasOwnProperty.call(cache, computed + '')
|
var inited = hasOwnProperty.call(cache, computed + '')
|
||||||
? !(seen = cache[computed])
|
? !(seen = cache[computed])
|
||||||
: (seen = []);
|
: (seen = cache[computed] = []);
|
||||||
}
|
}
|
||||||
if (isSorted
|
if (isSorted
|
||||||
? !index || seen[seen.length - 1] !== computed
|
? !index || seen[seen.length - 1] !== computed
|
||||||
|
|||||||
Reference in New Issue
Block a user