Skip to content

Commit

Permalink
Disable use of basicIndexOf optimization if _.indexOf is customized.
Browse files Browse the repository at this point in the history
Former-commit-id: 5b2273b36934581e34c6f6042de95bf556c61ca2
  • Loading branch information
jdalton committed May 27, 2013
1 parent 2e3b135 commit e9387d3
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 260 deletions.
34 changes: 20 additions & 14 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@
'cloneDeep': ['clone'],
'compact': [],
'compose': [],
'contains': ['basicEach', 'basicIndexOf', 'isString'],
'contains': ['basicEach', 'getIndexOf', 'isString'],
'countBy': ['createCallback', 'forEach'],
'createCallback': ['identity', 'isEqual', 'keys'],
'debounce': ['isObject'],
'defaults': ['createIterator', 'isArguments', 'keys'],
'defer': ['bind'],
'delay': [],
'difference': ['basicIndexOf', 'createCache'],
'difference': ['createCache'],
'escape': ['escapeHtmlChar'],
'every': ['basicEach', 'createCallback', 'isArray'],
'filter': ['basicEach', 'createCallback', 'isArray'],
Expand All @@ -113,7 +113,7 @@
'identity': [],
'indexOf': ['basicIndexOf', 'sortedIndex'],
'initial': ['slice'],
'intersection': ['basicIndexOf', 'createCache'],
'intersection': ['createCache'],
'invert': ['keys'],
'invoke': ['forEach'],
'isArguments': [],
Expand Down Expand Up @@ -143,7 +143,7 @@
'min': ['basicEach', 'charAtCallback', 'createCallback', 'isArray', 'isString'],
'mixin': ['forEach', 'functions'],
'noConflict': [],
'omit': ['basicIndexOf', 'forIn'],
'omit': ['forIn', 'getIndexOf'],
'once': [],
'pairs': ['keys'],
'parseInt': ['isString'],
Expand Down Expand Up @@ -172,7 +172,7 @@
'transform': ['createCallback', 'createObject', 'forOwn', 'isArray'],
'unescape': ['unescapeHtmlChar'],
'union': ['isArray', 'uniq'],
'uniq': ['basicIndexOf', 'createCache', 'overloadWrapper'],
'uniq': ['createCache', 'getIndexOf', 'overloadWrapper'],
'uniqueId': [],
'unzip': ['max', 'pluck'],
'value': ['basicEach', 'forOwn', 'isArray', 'lodashWrapper'],
Expand All @@ -189,11 +189,12 @@
'charAtCallback': [],
'compareAscending': [],
'createBound': ['createObject', 'isFunction', 'isObject'],
'createCache': ['basicIndexOf'],
'createCache': ['basicIndexOf', 'getIndexOf'],
'createIterator': ['iteratorTemplate'],
'createObject': [ 'isObject', 'noop'],
'escapeHtmlChar': [],
'escapeStringChar': [],
'getIndexOf': ['basicIndexOf', 'indexOf'],
'iteratorTemplate': [],
'isNode': [],
'lodashWrapper': [],
Expand Down Expand Up @@ -2277,10 +2278,11 @@
if (!useLodashMethod('contains')) {
source = replaceFunction(source, 'contains', [
'function contains(collection, target) {',
' var length = collection ? collection.length : 0,',
' var indexOf = getIndexOf(),',
' length = collection ? collection.length : 0,',
' result = false;',
" if (length && typeof length == 'number') {",
' result = basicIndexOf(collection, target) > -1;',
' result = indexOf(collection, target) > -1;',
' } else {',
' basicEach(collection, function(value) {',
' return !(result = value === target);',
Expand Down Expand Up @@ -2347,13 +2349,14 @@
source = replaceFunction(source, 'difference', [
'function difference(array) {',
' var index = -1,',
' indexOf = getIndexOf(),',
' length = array.length,',
' flattened = concat.apply(arrayProto, nativeSlice.call(arguments, 1)),',
' result = [];',
'',
' while (++index < length) {',
' var value = array[index];',
' if (basicIndexOf(flattened, value) < 0) {',
' if (indexOf(flattened, value) < 0) {',
' result.push(value);',
' }',
' }',
Expand Down Expand Up @@ -2388,16 +2391,17 @@
' var args = arguments,',
' argsLength = args.length,',
' index = -1,',
' indexOf = getIndexOf(),',
' length = array ? array.length : 0,',
' result = [];',
'',
' outer:',
' while (++index < length) {',
' var value = array[index];',
' if (basicIndexOf(result, value) < 0) {',
' if (indexOf(result, value) < 0) {',
' var argsIndex = argsLength;',
' while (--argsIndex) {',
' if (basicIndexOf(args[argsIndex], value) < 0) {',
' if (indexOf(args[argsIndex], value) < 0) {',
' continue outer;',
' }',
' }',
Expand Down Expand Up @@ -2547,11 +2551,12 @@
if (!useLodashMethod('omit')) {
source = replaceFunction(source, 'omit', [
'function omit(object) {',
' var props = concat.apply(arrayProto, nativeSlice.call(arguments, 1)),',
' var indexOf = getIndexOf(),',
' props = concat.apply(arrayProto, nativeSlice.call(arguments, 1)),',
' result = {};',
'',
' forIn(object, function(value, key) {',
' if (basicIndexOf(props, key) < 0) {',
' if (indexOf(props, key) < 0) {',
' result[key] = value;',
' }',
' });',
Expand Down Expand Up @@ -2716,6 +2721,7 @@
source = replaceFunction(source, 'uniq', [
'function uniq(array, isSorted, callback, thisArg) {',
' var index = -1,',
' indexOf = getIndexOf(),',
' length = array ? array.length : 0,',
' result = [],',
' seen = result;',
Expand All @@ -2735,7 +2741,7 @@
'',
' if (isSorted',
' ? !index || seen[seen.length - 1] !== computed',
' : basicIndexOf(seen, computed) < 0',
' : indexOf(seen, computed) < 0',
' ) {',
' if (callback) {',
' seen.push(computed);',
Expand Down
29 changes: 23 additions & 6 deletions dist/lodash.compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,9 @@

var bailout,
index = -1,
indexOf = getIndexOf(),
length = array.length,
isLarge = length >= largeArraySize,
isLarge = length >= largeArraySize && lodash.indexOf != indexOf,
objCache = {};

var caches = {
Expand All @@ -792,7 +793,7 @@
};

function basicContains(value) {
return basicIndexOf(array, value) > -1;
return indexOf(array, value) > -1;
}

function basicPush(value) {
Expand Down Expand Up @@ -938,6 +939,19 @@
return '\\' + stringEscapes[match];
}

/**
* Gets the appropriate "indexOf" function. If the `_.indexOf` method is
* customized, this method returns the custom method, otherwise it returns
* the `basicIndexOf` function.
*
* @private
* @returns {Function} Returns the "indexOf" function.
*/
function getIndexOf(array, value, fromIndex) {
var result = (result = lodash.indexOf) == indexOf ? basicIndexOf : result;
return result;
}

/**
* Checks if `value` is a DOM node in IE < 9.
*
Expand Down Expand Up @@ -2281,7 +2295,8 @@
* // => { 'name': 'moe' }
*/
function omit(object, callback, thisArg) {
var isFunc = typeof callback == 'function',
var indexOf = getIndexOf(),
isFunc = typeof callback == 'function',
result = {};

if (isFunc) {
Expand All @@ -2292,7 +2307,7 @@
forIn(object, function(value, key, object) {
if (isFunc
? !callback(value, key, object)
: basicIndexOf(props, key) < 0
: indexOf(props, key) < 0
) {
result[key] = value;
}
Expand Down Expand Up @@ -2518,14 +2533,15 @@
*/
function contains(collection, target, fromIndex) {
var index = -1,
indexOf = getIndexOf(),
length = collection ? collection.length : 0,
result = false;

fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
if (length && typeof length == 'number') {
result = (isString(collection)
? collection.indexOf(target, fromIndex)
: basicIndexOf(collection, target, fromIndex)
: indexOf(collection, target, fromIndex)
) > -1;
} else {
basicEach(collection, function(value) {
Expand Down Expand Up @@ -4221,6 +4237,7 @@
*/
var uniq = overloadWrapper(function(array, isSorted, callback) {
var index = -1,
indexOf = getIndexOf(),
length = array ? array.length : 0,
isLarge = !isSorted && length >= largeArraySize,
result = [],
Expand All @@ -4232,7 +4249,7 @@

if (isSorted
? !index || seen[seen.length - 1] !== computed
: (isLarge ? !seen.contains(computed) : basicIndexOf(seen, computed) < 0)
: (isLarge ? !seen.contains(computed) : indexOf(seen, computed) < 0)
) {
if (callback || isLarge) {
seen.push(computed);
Expand Down
Loading

0 comments on commit e9387d3

Please sign in to comment.