Optimize _.contains.

Former-commit-id: b50628adedd21fe2c099f4dcab1aa9f0036a22ed
This commit is contained in:
John-David Dalton
2012-11-10 23:09:23 -08:00
parent 3b9bd944fb
commit 07cbfdb424
5 changed files with 74 additions and 62 deletions

View File

@@ -1836,18 +1836,23 @@
*/
function contains(collection, target, fromIndex) {
var index = -1,
length = collection ? collection.length : 0;
length = collection ? collection.length : 0,
result = false;
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
if (typeof length == 'number') {
return (isString(collection)
result = (isString(collection)
? collection.indexOf(target, fromIndex)
: indexOf(collection, target, fromIndex)
) > -1;
} else {
forEach(collection, function(value) {
if (++index >= fromIndex) {
return !(result = value === target);
}
});
}
return some(collection, function(value) {
return ++index >= fromIndex && value === target;
});
return result;
}
/**