8000 Optimize `_.intersection`, move `largeSize` default to `largeArraySiz… · lodash/lodash@c02c2d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c02c2d3

Browse files
committed
Optimize _.intersection, move largeSize default to largeArraySize, and cleanup _.where.
Former-commit-id: 9eaea7922623f1bd69f2b18578468a6fc9ba13fc
1 parent 7adf5e7 commit c02c2d3

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
'identity': [],
187187
'indexOf': ['sortedIndex'],
188188
'initial': [],
189-
'intersection': ['every', 'indexOf'],
189+
'intersection': ['indexOf'],
190190
'invoke': [],
191191
'isArguments': [],
192192
'isArray': [],

build/pre-compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/** Used to minify variables embedded in compiled strings */
99
var compiledVars = [
10+
'argsIndex',
11+
'argsLength',
1012
'callback',
1113
'collection',
1214
'concat',
@@ -36,8 +38,6 @@
3638
// lesser used variables
3739
'accumulator',
3840
'args',
39-
'argsIndex',
40-
'argsLength',
4141
'arrayLikeClasses',
4242
'ArrayProto',
4343
'bind',

lodash.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
/** Used to generate unique IDs */
4848
var idCounter = 0;
4949

50+
/** Used by `cachedContains` as the default size when optimizations are enabled for large arrays */
51+
var largeArraySize = 30;
52+
5053
/** Used to restore the original `_` reference in `noConflict` */
5154
var oldDash = window._;
5255

@@ -548,7 +551,7 @@
548551
fromIndex || (fromIndex = 0);
549552

550553
var length = array.length,
551-
isLarge = (length - fromIndex) >= (largeSize || 30),
554+
isLarge = (length - fromIndex) >= (largeSize || largeArraySize),
552555
cache = isLarge ? {} : array;
553556

554557
if (isLarge) {
@@ -2365,15 +2368,15 @@
23652368
var where = createIterator(filterIteratorOptions, {
23662369
'args': 'collection, properties',
23672370
'top':
2368-
'var pass, prop, propIndex, props = [];\n' +
2371+
'var props = [];\n' +
23692372
'forIn(properties, function(value, prop) { props.push(prop) });\n' +
23702373
'var propsLength = props.length',
23712374
'inLoop':
2372-
'for (pass = true, propIndex = 0; propIndex < propsLength; propIndex++) {\n' +
2375+
'for (var prop, pass = true, propIndex = 0; propIndex < propsLength; propIndex++) {\n' +
23732376
' prop = props[propIndex];\n' +
23742377
' if (!(pass = value[prop] === properties[prop])) break\n' +
23752378
'}\n' +
2376-
'if (pass) result.push(value)'
2379+
'pass && result.push(value)'
23772380
});
23782381

23792382
/*--------------------------------------------------------------------------*/
@@ -2597,17 +2600,19 @@
25972600
return result;
25982601
}
25992602
var value,
2603+
argsLength = arguments.length,
2604+
cache = [],
26002605
index = -1,
2601-
length = array.length,
2602-
others = slice.call(arguments, 1),
2603-
cache = [];
2606+
length = array.length;
26042607

2605-
while (++index < length) {
2608+
array: while (++index < length) {
26062609
value = array[index];
2607-
if (indexOf(result, value) < 0 &&
2608-
every(others, function(other, index) {
2609-
return (cache[index] || (cache[index] = cachedContains(other)))(value);
2610-
})) {
2610+
if (indexOf(result, value) < 0) {
2611+
for (var argsIndex = 1; argsIndex < argsLength; argsIndex++) {
2612+
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(arguments[argsIndex])))(value)) {
2613+
continue array;
2614+
}
2615+
}
26112616
result.push(value);
26122617
}
26132618
}

0 commit comments

Comments
 (0)
0