mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
Add baseDifference to optimize _.omit & _.without.
This commit is contained in:
71
lodash.js
71
lodash.js
@@ -1249,6 +1249,43 @@
|
||||
return bound;
|
||||
}
|
||||
|
||||
/**
|
||||
* The base implementation of `_.difference` that accepts a single array
|
||||
* of values to exclude.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The array to process.
|
||||
* @param {Array} [values] The array of values to exclude.
|
||||
* @returns {Array} Returns a new array of filtered values.
|
||||
*/
|
||||
function baseDifference(array, values) {
|
||||
var index = -1,
|
||||
indexOf = getIndexOf(),
|
||||
length = array ? array.length : 0,
|
||||
isLarge = length >= largeArraySize && indexOf === baseIndexOf,
|
||||
result = [];
|
||||
|
||||
if (isLarge) {
|
||||
var cache = createCache(values);
|
||||
if (cache) {
|
||||
indexOf = cacheIndexOf;
|
||||
values = cache;
|
||||
} else {
|
||||
isLarge = false;
|
||||
}
|
||||
}
|
||||
while (++index < length) {
|
||||
var value = array[index];
|
||||
if (indexOf(values, value) < 0) {
|
||||
result.push(value);
|
||||
}
|
||||
}
|
||||
if (isLarge) {
|
||||
releaseObject(values);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The base implementation of `_.flatten` without support for callback
|
||||
* shorthands or `thisArg` binding.
|
||||
@@ -2995,7 +3032,7 @@
|
||||
forIn(object, function(value, key) {
|
||||
props.push(key);
|
||||
});
|
||||
props = difference(props, baseFlatten(arguments, true, false, 1));
|
||||
props = baseDifference(props, baseFlatten(arguments, true, false, 1));
|
||||
|
||||
var index = -1,
|
||||
length = props.length;
|
||||
@@ -4374,7 +4411,7 @@
|
||||
* @memberOf _
|
||||
* @category Arrays
|
||||
* @param {Array} array The array to process.
|
||||
* @param {...Array} [array] The arrays of values to exclude.
|
||||
* @param {...Array} [values] The arrays of values to exclude.
|
||||
* @returns {Array} Returns a new array of filtered values.
|
||||
* @example
|
||||
*
|
||||
@@ -4382,33 +4419,7 @@
|
||||
* // => [1, 3, 4]
|
||||
*/
|
||||
function difference(array) {
|
||||
var index = -1,
|
||||
indexOf = getIndexOf(),
|
||||
length = array ? array.length : 0,
|
||||
seen = baseFlatten(arguments, true, true, 1),
|
||||
result = [];
|
||||
|
||||
var isLarge = length >= largeArraySize && indexOf === baseIndexOf;
|
||||
|
||||
if (isLarge) {
|
||||
var cache = createCache(seen);
|
||||
if (cache) {
|
||||
indexOf = cacheIndexOf;
|
||||
seen = cache;
|
||||
} else {
|
||||
isLarge = false;
|
||||
}
|
||||
}
|
||||
while (++index < length) {
|
||||
var value = array[index];
|
||||
if (indexOf(seen, value) < 0) {
|
||||
result.push(value);
|
||||
}
|
||||
}
|
||||
if (isLarge) {
|
||||
releaseObject(seen);
|
||||
}
|
||||
return result;
|
||||
return baseDifference(array, baseFlatten(arguments, true, true, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5272,7 +5283,7 @@
|
||||
* // => [2, 3, 4]
|
||||
*/
|
||||
function without(array) {
|
||||
return difference(array, slice(arguments, 1));
|
||||
return baseDifference(array, slice(arguments, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user