Optimize _.intersection, move largeSize default to largeArraySize, and cleanup _.where.

Former-commit-id: 9eaea7922623f1bd69f2b18578468a6fc9ba13fc
This commit is contained in:
John-David Dalton
2012-08-25 21:50:08 -07:00
parent 7adf5e763b
commit c02c2d3b2c
3 changed files with 20 additions and 15 deletions

View File

@@ -186,7 +186,7 @@
'identity': [], 'identity': [],
'indexOf': ['sortedIndex'], 'indexOf': ['sortedIndex'],
'initial': [], 'initial': [],
'intersection': ['every', 'indexOf'], 'intersection': ['indexOf'],
'invoke': [], 'invoke': [],
'isArguments': [], 'isArguments': [],
'isArray': [], 'isArray': [],

View File

@@ -7,6 +7,8 @@
/** Used to minify variables embedded in compiled strings */ /** Used to minify variables embedded in compiled strings */
var compiledVars = [ var compiledVars = [
'argsIndex',
'argsLength',
'callback', 'callback',
'collection', 'collection',
'concat', 'concat',
@@ -36,8 +38,6 @@
// lesser used variables // lesser used variables
'accumulator', 'accumulator',
'args', 'args',
'argsIndex',
'argsLength',
'arrayLikeClasses', 'arrayLikeClasses',
'ArrayProto', 'ArrayProto',
'bind', 'bind',

View File

@@ -47,6 +47,9 @@
/** Used to generate unique IDs */ /** Used to generate unique IDs */
var idCounter = 0; 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` */ /** Used to restore the original `_` reference in `noConflict` */
var oldDash = window._; var oldDash = window._;
@@ -548,7 +551,7 @@
fromIndex || (fromIndex = 0); fromIndex || (fromIndex = 0);
var length = array.length, var length = array.length,
isLarge = (length - fromIndex) >= (largeSize || 30), isLarge = (length - fromIndex) >= (largeSize || largeArraySize),
cache = isLarge ? {} : array; cache = isLarge ? {} : array;
if (isLarge) { if (isLarge) {
@@ -2365,15 +2368,15 @@
var where = createIterator(filterIteratorOptions, { var where = createIterator(filterIteratorOptions, {
'args': 'collection, properties', 'args': 'collection, properties',
'top': 'top':
'var pass, prop, propIndex, props = [];\n' + 'var props = [];\n' +
'forIn(properties, function(value, prop) { props.push(prop) });\n' + 'forIn(properties, function(value, prop) { props.push(prop) });\n' +
'var propsLength = props.length', 'var propsLength = props.length',
'inLoop': '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' + ' prop = props[propIndex];\n' +
' if (!(pass = value[prop] === properties[prop])) break\n' + ' if (!(pass = value[prop] === properties[prop])) break\n' +
'}\n' + '}\n' +
'if (pass) result.push(value)' 'pass && result.push(value)'
}); });
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -2597,17 +2600,19 @@
return result; return result;
} }
var value, var value,
argsLength = arguments.length,
cache = [],
index = -1, index = -1,
length = array.length, length = array.length;
others = slice.call(arguments, 1),
cache = [];
while (++index < length) { array: while (++index < length) {
value = array[index]; value = array[index];
if (indexOf(result, value) < 0 && if (indexOf(result, value) < 0) {
every(others, function(other, index) { for (var argsIndex = 1; argsIndex < argsLength; argsIndex++) {
return (cache[index] || (cache[index] = cachedContains(other)))(value); if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(arguments[argsIndex])))(value)) {
})) { continue array;
}
}
result.push(value); result.push(value);
} }
} }