Add common case paths to _.difference and _.uniq.

This commit is contained in:
John-David Dalton
2014-02-23 23:46:21 -08:00
parent f313752888
commit 303215d42f
2 changed files with 41 additions and 13 deletions

View File

@@ -19,9 +19,6 @@
PARTIAL_FLAG = 16, PARTIAL_FLAG = 16,
PARTIAL_RIGHT_FLAG = 32; PARTIAL_RIGHT_FLAG = 32;
/** Used as the size when optimizations are enabled for arrays */
var LARGE_ARRAY_SIZE = 40;
/** Used as the semantic version number */ /** Used as the semantic version number */
var version = '2.4.1'; var version = '2.4.1';
@@ -1294,15 +1291,30 @@
} }
var index = -1, var index = -1,
indexOf = getIndexOf(), indexOf = getIndexOf(),
result = []; prereq = indexOf === baseIndexOf,
isLarge = prereq && createCache && values && values.length >= 200,
isCommon = prereq && !isLarge,
result = [],
valuesLength = values ? values.length : 0;
if (createCache && values && indexOf === baseIndexOf && values.length >= LARGE_ARRAY_SIZE) { if (isLarge) {
indexOf = cacheIndexOf; indexOf = cacheIndexOf;
values = createCache(values); values = createCache(values);
} }
outer:
while (++index < length) { while (++index < length) {
var value = array[index]; var value = array[index];
if (indexOf(values, value) < 0) {
if (isCommon) {
var valuesIndex = valuesLength;
while (valuesIndex--) {
if (values[valuesIndex] === value) {
continue outer;
}
}
result.push(value);
}
else if (indexOf(values, value) < 0) {
result.push(value); result.push(value);
} }
} }
@@ -1711,7 +1723,9 @@
} }
var index = -1, var index = -1,
indexOf = getIndexOf(), indexOf = getIndexOf(),
isLarge = createCache && !isSorted && indexOf === baseIndexOf && length >= LARGE_ARRAY_SIZE, prereq = !isSorted && indexOf === baseIndexOf,
isLarge = prereq && createCache && length >= 200,
isCommon = prereq && !isLarge,
result = []; result = [];
if (isLarge) { if (isLarge) {
@@ -1720,16 +1734,30 @@
} else { } else {
seen = (callback && !isSorted) ? [] : result; seen = (callback && !isSorted) ? [] : result;
} }
outer:
while (++index < length) { while (++index < length) {
var value = array[index], var value = array[index],
computed = callback ? callback(value, index, array) : value; computed = callback ? callback(value, index, array) : value;
if (isSorted) { if (isCommon) {
var seenIndex = seen.length;
while (seenIndex--) {
if (seen[seenIndex] === computed) {
continue outer;
}
}
if (callback) {
seen.push(computed);
}
result.push(value);
}
else if (isSorted) {
if (!index || seen !== computed) { if (!index || seen !== computed) {
seen = computed; seen = computed;
result.push(value); result.push(value);
} }
} else if (indexOf(seen, computed) < 0) { }
else if (indexOf(seen, computed) < 0) {
if (callback || isLarge) { if (callback || isLarge) {
seen.push(computed); seen.push(computed);
} }
@@ -2566,14 +2594,14 @@
argsLength = arguments.length, argsLength = arguments.length,
caches = [], caches = [],
indexOf = getIndexOf(), indexOf = getIndexOf(),
largePrereq = createCache && indexOf === baseIndexOf, prereq = createCache && indexOf === baseIndexOf,
seen = []; 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(largePrereq && value.length >= LARGE_ARRAY_SIZE && caches.push(prereq && value.length >= 120 &&
createCache(argsIndex ? args[argsIndex] : seen)); createCache(argsIndex ? args[argsIndex] : seen));
} }
} }

View File

@@ -3,8 +3,8 @@
/** Used as a safe reference for `undefined` in pre ES5 environments */ /** Used as a safe reference for `undefined` in pre ES5 environments */
var undefined; var undefined;
/** Used as the size when optimizations are enabled for arrays */ /** Used as the size to cover large array optimizations */
var LARGE_ARRAY_SIZE = 75; var LARGE_ARRAY_SIZE = 200;
/** Used as a reference to the global object */ /** Used as a reference to the global object */
var root = typeof global == 'object' && global || this; var root = typeof global == 'object' && global || this;