8000 Use `nativeSlice` when possible and adjust `largeArraySize` to accoun… · lodash/lodash@e97e645 · GitHub
[go: up one dir, main page]

Skip to content

Commit e97e645

Browse files
committed
Use nativeSlice when possible and adjust largeArraySize to account for the recent cachedContains tweaks.
Former-commit-id: 9fe4dc10c74fb7a4b8e5cff434a4146d274f15d4
1 parent 4a03ba3 commit e97e645

10 files changed

+338
-390
lines changed

build.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
'value': ['forOwn', 'isArray'],
176176
'values': ['keys'],
177177
'where': ['filter'],
178-
'without': ['indexOf'],
178+
'without': ['difference'],
179179
'wrap': [],
180180
'zip': ['max', 'pluck'],
181181
'zipObject': [],
@@ -2392,23 +2392,6 @@
23922392
'}'
23932393
].join('\n'));
23942394

2395-
// replace `_.without`
2396-
source = replaceFunction(source, 'without', [
2397-
'function without(array) {',
2398-
' var index = -1,',
2399-
' length = array.length,',
2400-
' result = [];',
2401-
'',
2402-
' while (++index < length) {',
2403-
' var value = array[index];',
2404-
' if (indexOf(arguments, value, 1) < 0) {',
2405-
' result.push(value);',
2406-
' }',
2407-
' }',
2408-
' return result',
2409-
'}'
2410-
].join('\n'));
2411-
24122395
// add `_.findWhere`
24132396
source = source.replace(matchFunction(source, 'find'), function(match) {
24142397
var indent = getIndent(match);
@@ -2446,10 +2429,9 @@
24462429
});
24472430
});
24482431

2449-
// replace `slice` with `slice.call`
2432+
// replace `slice` with `nativeSlice.call`
24502433
source = removeFunction(source, 'slice');
2451-
source = source.replace(/^(( *)setTimeout = context.setTimeout)([,;])/m, '$1,\n$2slice = arrayRef.slice$3');
2452-
source = source.replace(/([^.]\bslice)\(/g, '$1.call(');
2434+
source = source.replace(/([^.])\bslice\(/g, '$1nativeSlice.call(');
24532435

24542436
// replace `lodash.createCallback` references with `createCallback`
24552437
if (!exposeCreateCallback) {

dist/lodash.compat.js

Lines changed: 21 additions & 29 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
/** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
3434
var keyPrefix = +new Date + '';
3535

36+
/** Used as the size when optimizations are enabled for large arrays */
37+
var largeArraySize = 200;
38+
3639
/** Used to match empty string literals in compiled template source */
3740
var reEmptyStringLeading = /\b__p \+= '';/g,
3841
reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
@@ -186,7 +189,8 @@
186189
nativeMax = Math.max,
187190
nativeMin = Math.min,
188191
nativeParseInt = context.parseInt,
189-
nativeRandom = Math.random;
192+
nativeRandom = Math.random,
193+
nativeSlice = arrayRef.slice;
190194

191195
/** Detect various environments */
192196
var isIeOpera = reNative.test(context.attachEvent),
@@ -570,12 +574,11 @@
570574
* @param {Array} array The array to search.
571575
* @param {Mixed} value The value to search for.
572576
* @param {Number} fromIndex The index to search from.
573-
* @param {Number} largeSize The length at which an array is considered large.
574577
* @returns {Boolean} Returns `true`, if `value` is found, else `false`.
575578
*/
576-
function cachedContains(array, fromIndex, largeSize) {
579+
function cachedContains(array, fromIndex) {
577580
var length = array.length,
578-
isLarge = (length - fromIndex) >= largeSize;
581+
isLarge = (length - fromIndex) >= largeArraySize;
579582

580583
if (isLarge) {
581584
var cache = {},
@@ -677,7 +680,7 @@
677680
}
678681
if (partialArgs.length) {
679682
args = args.length
680-
? (args = slice(args), rightIndicator ? args.concat(partialArgs) : partialArgs.concat(args))
683+
? (args = nativeSlice.call(args), rightIndicator ? args.concat(partialArgs) : partialArgs.concat(args))
681684
: partialArgs;
682685
}
683686
if (this instanceof bound) {
@@ -2237,7 +2240,7 @@
22372240
*/
22382241
function at(collection) {
22392242
var index = -1,
2240-
props = concat.apply(arrayRef, slice(arguments, 1)),
2243+
props = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),
22412244
length = props.length,
22422245
result = Array(length);
22432246

@@ -2642,7 +2645,7 @@
26422645
* // => [['1', '2', '3'], ['4', '5', '6']]
26432646
*/
26442647
function invoke(collection, methodName) {
2645-
var args = slice(arguments, 2),
2648+
var args = nativeSlice.call(arguments, 2),
26462649
index = -1,
26472650
isFunc = typeof methodName == 'function',
26482651
length = collection ? collection.length : 0,
@@ -3280,7 +3283,7 @@
32803283
var index = -1,
32813284
length = array ? array.length : 0,
32823285
flattened = concat.apply(arrayRef, arguments),
3283-
contains = cachedContains(flattened, length, 100),
3286+
contains = cachedContains(flattened, length),
32843287
result = [];
32853288

32863289
while (++index < length) {
@@ -3611,7 +3614,7 @@
36113614
cache = { '0': {} },
36123615
index = -1,
36133616
length = array ? array.length : 0,
3614-
isLarge = length >= 100,
3617+
isLarge = length >= largeArraySize,
36153618
result = [],
36163619
seen = result;
36173620

@@ -3630,7 +3633,7 @@
36303633
}
36313634
var argsIndex = argsLength;
36323635
while (--argsIndex) {
3633-
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex], 0, 100)))(value)) {
3636+
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex], 0)))(value)) {
36343637
continue outer;
36353638
}
36363639
}
@@ -4014,7 +4017,7 @@
40144017
isSorted = false;
40154018
}
40164019
// init value cache for large arrays
4017-
var isLarge = !isSorted && length >= 75;
4020+
var isLarge = !isSorted && length >= largeArraySize;
40184021
if (isLarge) {
40194022
var cache = {};
40204023
}
@@ -4092,18 +4095,7 @@
40924095
* // => [2, 3, 4]
40934096
*/
40944097
function without(array) {
4095-
var index = -1,
4096-
length = array ? array.length : 0,
4097-
contains = cachedContains(arguments, 1, 30),
4098-
result = [];
4099-
4100-
while (++index < length) {
4101-
var value = array[index];
4102-
if (!contains(value)) {
4103-
result.push(value);
4104-
}
4105-
}
4106-
return result;
4098+
return difference(array, nativeSlice.call(arguments, 1));
41074099
}
41084100

41094101
/**
@@ -4229,7 +4221,7 @@
42294221
// (in V8 `Function#bind` is slower except when partially applied)
42304222
return support.fastBind || (nativeBind && arguments.length > 2)
42314223
? nativeBind.call.apply(nativeBind, arguments)
4232-
: createBound(func, thisArg, slice(arguments, 2));
4224+
: createBound(func, thisArg, nativeSlice.call(arguments, 2));
42334225
}
42344226

42354227
/**
@@ -4302,7 +4294,7 @@
43024294
* // => 'hi, moe!'
43034295
*/
43044296
function bindKey(object, key) {
4305-
return createBound(object, key, slice(arguments, 2), indicatorObject);
4297+
return createBound(object, key, nativeSlice.call(arguments, 2), indicatorObject);
43064298
}
43074299

43084300
/**
@@ -4501,7 +4493,7 @@
45014493
* // returns from the function before `alert` is called
45024494
*/
45034495
function defer(func) {
4504-
var args = slice(arguments, 1);
4496+
var args = nativeSlice.call(arguments, 1);
45054497
return setTimeout(function() { func.apply(undefined, args); }, 1);
45064498
}
45074499
// use `setImmediate` if it's available in Node.js
@@ -4527,7 +4519,7 @@
45274519
* // => 'logged later' (Appears after one second.)
45284520
*/
45294521
function delay(func, wait) {
4530-
var args = slice(arguments, 2);
4522+
var args = nativeSlice.call(arguments, 2);
45314523
return setTimeout(function() { func.apply(undefined, args); }, wait);
45324524
}
45334525

@@ -4613,7 +4605,7 @@
46134605
* // => 'hi moe'
46144606
*/
46154607
function partial(func) {
4616-
return createBound(func, slice(arguments, 1));
4608+
return createBound(func, nativeSlice.call(arguments, 1));
46174609
}
46184610

46194611
/**
@@ -4644,7 +4636,7 @@
46444636
* // => { '_': _, 'jq': $ }
46454637
*/
46464638
function partialRight(func) {
4647-
return createBound(func, slice(arguments, 1), null, indicatorObject);
4639+
return createBound(func, nativeSlice.call(arguments, 1), null, indicatorObject);
46484640
}
46494641

46504642
/**

0 commit comments

Comments
 (0)
0