Optimize _.omit for large arrays by leveraging _.difference which has optimizations for large arrays.

This commit is contained in:
John-David Dalton
2013-11-06 20:45:24 -08:00
parent 7e7b129822
commit 3dd3af4a73
7 changed files with 108 additions and 85 deletions

36
dist/lodash.js vendored
View File

@@ -2631,23 +2631,29 @@
* // => { 'name': 'fred' }
*/
function omit(object, callback, thisArg) {
var indexOf = getIndexOf(),
isFunc = typeof callback == 'function',
result = {};
var result = {};
if (typeof callback != 'function') {
var props = [];
forIn(object, function(value, key) {
props.push(key);
});
props = difference(props, baseFlatten(arguments, true, false, 1));
if (isFunc) {
callback = lodash.createCallback(callback, thisArg, 3);
} else {
var props = baseFlatten(arguments, true, false, 1);
}
forIn(object, function(value, key, object) {
if (isFunc
? !callback(value, key, object)
: indexOf(props, key) < 0
) {
result[key] = value;
var index = -1,
length = props.length;
while (++index < length) {
var key = props[index];
result[key] = object[key];
}
});
} else {
callback = lodash.createCallback(callback, thisArg, 3);
forIn(object, function(value, key, object) {
if (!callback(value, key, object)) {
result[key] = value;
}
});
}
return result;
}