mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Optimize _.contains by using native String#contains when available.
This commit is contained in:
39
lodash.js
39
lodash.js
@@ -527,7 +527,8 @@
|
||||
}());
|
||||
|
||||
/* Native method shortcuts for methods with the same name as other `lodash` methods */
|
||||
var nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate,
|
||||
var nativeContains = isNative(nativeContains = stringProto.contains) && nativeContains,
|
||||
nativeCreate = isNative(nativeCreate = Object.create) && nativeCreate,
|
||||
nativeIsArray = isNative(nativeIsArray = Array.isArray) && nativeIsArray,
|
||||
nativeIsFinite = context.isFinite,
|
||||
nativeIsNaN = context.isNaN,
|
||||
@@ -3357,23 +3358,31 @@
|
||||
* // => true
|
||||
*/
|
||||
function contains(collection, target, fromIndex) {
|
||||
var length = collection ? collection.length : 0;
|
||||
fromIndex = typeof fromIndex == 'number' ? fromIndex : 0;
|
||||
|
||||
if (typeof length == 'number') {
|
||||
if (fromIndex >= length) {
|
||||
return false;
|
||||
}
|
||||
if (typeof collection == 'string' || !isArray(collection) && isString(collection)) {
|
||||
return nativeContains
|
||||
? nativeContains.call(collection, target, fromIndex)
|
||||
: collection.indexOf(target, fromIndex) > -1;
|
||||
}
|
||||
var indexOf = getIndexOf();
|
||||
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
|
||||
return indexOf(collection, target, fromIndex) > -1;
|
||||
}
|
||||
var index = -1,
|
||||
indexOf = getIndexOf(),
|
||||
length = collection ? collection.length : 0,
|
||||
result = false;
|
||||
|
||||
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
|
||||
if (isArray(collection)) {
|
||||
result = indexOf(collection, target, fromIndex) > -1;
|
||||
} else if (typeof length == 'number') {
|
||||
result = (isString(collection) ? collection.indexOf(target, fromIndex) : indexOf(collection, target, fromIndex)) > -1;
|
||||
} else {
|
||||
baseEach(collection, function(value) {
|
||||
if (++index >= fromIndex) {
|
||||
return !(result = value === target);
|
||||
}
|
||||
});
|
||||
}
|
||||
baseEach(collection, function(value) {
|
||||
if (++index >= fromIndex) {
|
||||
return !(result = value === target);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user