Optimize _.uniq for large arrays.

Former-commit-id: 6c739aab6bcb8c31f9db9821d6eaf50c9a4fb80b
This commit is contained in:
John-David Dalton
2012-11-02 11:56:08 -07:00
parent 96f8f2891b
commit b17c576705
4 changed files with 89 additions and 43 deletions

View File

@@ -436,7 +436,7 @@
/*--------------------------------------------------------------------------*/
/**
* Creates a function optimized for searching large arrays for a given `value`,
* Creates a function optimized to search large arrays for a given `value`,
* starting at `fromIndex`, using strict equality for comparisons, i.e. `===`.
*
* @private
@@ -2981,6 +2981,11 @@
callback = isSorted;
isSorted = false;
}
// init value cache for large arrays
var isLarge = !isSorted && length > 74;
if (isLarge) {
var cache = {};
}
if (callback) {
seen = [];
callback = createCallback(callback, thisArg);
@@ -2989,11 +2994,15 @@
var value = array[index],
computed = callback ? callback(value, index, array) : value;
if (isLarge) {
var key = computed + '';
seen = hasOwnProperty.call(cache, key) ? cache[key] : (cache[key] = []);
}
if (isSorted
? !index || seen[seen.length - 1] !== computed
: indexOf(seen, computed) < 0
) {
if (callback) {
if (callback || isLarge) {
seen.push(computed);
}
result.push(value);