From c02c2d3b2c090ad3d0490633b4e5f5df5e7de92e Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 25 Aug 2012 21:50:08 -0700 Subject: [PATCH] Optimize `_.intersection`, move `largeSize` default to `largeArraySize`, and cleanup `_.where`. Former-commit-id: 9eaea7922623f1bd69f2b18578468a6fc9ba13fc --- build.js | 2 +- build/pre-compile.js | 4 ++-- lodash.js | 29 +++++++++++++++++------------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/build.js b/build.js index acbd22a95..57aeea51b 100755 --- a/build.js +++ b/build.js @@ -186,7 +186,7 @@ 'identity': [], 'indexOf': ['sortedIndex'], 'initial': [], - 'intersection': ['every', 'indexOf'], + 'intersection': ['indexOf'], 'invoke': [], 'isArguments': [], 'isArray': [], diff --git a/build/pre-compile.js b/build/pre-compile.js index aa65069ad..db395d6d2 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -7,6 +7,8 @@ /** Used to minify variables embedded in compiled strings */ var compiledVars = [ + 'argsIndex', + 'argsLength', 'callback', 'collection', 'concat', @@ -36,8 +38,6 @@ // lesser used variables 'accumulator', 'args', - 'argsIndex', - 'argsLength', 'arrayLikeClasses', 'ArrayProto', 'bind', diff --git a/lodash.js b/lodash.js index febc06a51..4e163dd9f 100644 --- a/lodash.js +++ b/lodash.js @@ -47,6 +47,9 @@ /** Used to generate unique IDs */ var idCounter = 0; + /** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */ + var largeArraySize = 30; + /** Used to restore the original `_` reference in `noConflict` */ var oldDash = window._; @@ -548,7 +551,7 @@ fromIndex || (fromIndex = 0); var length = array.length, - isLarge = (length - fromIndex) >= (largeSize || 30), + isLarge = (length - fromIndex) >= (largeSize || largeArraySize), cache = isLarge ? {} : array; if (isLarge) { @@ -2365,15 +2368,15 @@ var where = createIterator(filterIteratorOptions, { 'args': 'collection, properties', 'top': - 'var pass, prop, propIndex, props = [];\n' + + 'var props = [];\n' + 'forIn(properties, function(value, prop) { props.push(prop) });\n' + 'var propsLength = props.length', 'inLoop': - 'for (pass = true, propIndex = 0; propIndex < propsLength; propIndex++) {\n' + + 'for (var prop, pass = true, propIndex = 0; propIndex < propsLength; propIndex++) {\n' + ' prop = props[propIndex];\n' + ' if (!(pass = value[prop] === properties[prop])) break\n' + '}\n' + - 'if (pass) result.push(value)' + 'pass && result.push(value)' }); /*--------------------------------------------------------------------------*/ @@ -2597,17 +2600,19 @@ return result; } var value, + argsLength = arguments.length, + cache = [], index = -1, - length = array.length, - others = slice.call(arguments, 1), - cache = []; + length = array.length; - while (++index < length) { + array: while (++index < length) { value = array[index]; - if (indexOf(result, value) < 0 && - every(others, function(other, index) { - return (cache[index] || (cache[index] = cachedContains(other)))(value); - })) { + if (indexOf(result, value) < 0) { + for (var argsIndex = 1; argsIndex < argsLength; argsIndex++) { + if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(arguments[argsIndex])))(value)) { + continue array; + } + } result.push(value); } }