mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Use nativeSlice when possible and adjust largeArraySize to account for the recent cachedContains tweaks.
Former-commit-id: 9fe4dc10c74fb7a4b8e5cff434a4146d274f15d4
This commit is contained in:
50
lodash.js
50
lodash.js
@@ -32,6 +32,9 @@
|
||||
/** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
|
||||
var keyPrefix = +new Date + '';
|
||||
|
||||
/** Used as the size when optimizations are enabled for large arrays */
|
||||
var largeArraySize = 200;
|
||||
|
||||
/** Used to match empty string literals in compiled template source */
|
||||
var reEmptyStringLeading = /\b__p \+= '';/g,
|
||||
reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
|
||||
@@ -185,7 +188,8 @@
|
||||
nativeMax = Math.max,
|
||||
nativeMin = Math.min,
|
||||
nativeParseInt = context.parseInt,
|
||||
nativeRandom = Math.random;
|
||||
nativeRandom = Math.random,
|
||||
nativeSlice = arrayRef.slice;
|
||||
|
||||
/** Detect various environments */
|
||||
var isIeOpera = reNative.test(context.attachEvent),
|
||||
@@ -577,12 +581,11 @@
|
||||
* @param {Array} array The array to search.
|
||||
* @param {Mixed} value The value to search for.
|
||||
* @param {Number} fromIndex The index to search from.
|
||||
* @param {Number} largeSize The length at which an array is considered large.
|
||||
* @returns {Boolean} Returns `true`, if `value` is found, else `false`.
|
||||
*/
|
||||
function cachedContains(array, fromIndex, largeSize) {
|
||||
function cachedContains(array, fromIndex) {
|
||||
var length = array.length,
|
||||
isLarge = (length - fromIndex) >= largeSize;
|
||||
isLarge = (length - fromIndex) >= largeArraySize;
|
||||
|
||||
if (isLarge) {
|
||||
var cache = {},
|
||||
@@ -684,7 +687,7 @@
|
||||
}
|
||||
if (partialArgs.length) {
|
||||
args = args.length
|
||||
? (args = slice(args), rightIndicator ? args.concat(partialArgs) : partialArgs.concat(args))
|
||||
? (args = nativeSlice.call(args), rightIndicator ? args.concat(partialArgs) : partialArgs.concat(args))
|
||||
: partialArgs;
|
||||
}
|
||||
if (this instanceof bound) {
|
||||
@@ -2247,7 +2250,7 @@
|
||||
*/
|
||||
function at(collection) {
|
||||
var index = -1,
|
||||
props = concat.apply(arrayRef, slice(arguments, 1)),
|
||||
props = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),
|
||||
length = props.length,
|
||||
result = Array(length);
|
||||
|
||||
@@ -2652,7 +2655,7 @@
|
||||
* // => [['1', '2', '3'], ['4', '5', '6']]
|
||||
*/
|
||||
function invoke(collection, methodName) {
|
||||
var args = slice(arguments, 2),
|
||||
var args = nativeSlice.call(arguments, 2),
|
||||
index = -1,
|
||||
isFunc = typeof methodName == 'function',
|
||||
length = collection ? collection.length : 0,
|
||||
@@ -3290,7 +3293,7 @@
|
||||
var index = -1,
|
||||
length = array ? array.length : 0,
|
||||
flattened = concat.apply(arrayRef, arguments),
|
||||
contains = cachedContains(flattened, length, 100),
|
||||
contains = cachedContains(flattened, length),
|
||||
result = [];
|
||||
|
||||
while (++index < length) {
|
||||
@@ -3621,7 +3624,7 @@
|
||||
cache = { '0': {} },
|
||||
index = -1,
|
||||
length = array ? array.length : 0,
|
||||
isLarge = length >= 100,
|
||||
isLarge = length >= largeArraySize,
|
||||
result = [],
|
||||
seen = result;
|
||||
|
||||
@@ -3640,7 +3643,7 @@
|
||||
}
|
||||
var argsIndex = argsLength;
|
||||
while (--argsIndex) {
|
||||
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex], 0, 100)))(value)) {
|
||||
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex], 0)))(value)) {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
@@ -4024,7 +4027,7 @@
|
||||
isSorted = false;
|
||||
}
|
||||
// init value cache for large arrays
|
||||
var isLarge = !isSorted && length >= 75;
|
||||
var isLarge = !isSorted && length >= largeArraySize;
|
||||
if (isLarge) {
|
||||
var cache = {};
|
||||
}
|
||||
@@ -4102,18 +4105,7 @@
|
||||
* // => [2, 3, 4]
|
||||
*/
|
||||
function without(array) {
|
||||
var index = -1,
|
||||
length = array ? array.length : 0,
|
||||
contains = cachedContains(arguments, 1, 30),
|
||||
result = [];
|
||||
|
||||
while (++index < length) {
|
||||
var value = array[index];
|
||||
if (!contains(value)) {
|
||||
result.push(value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return difference(array, nativeSlice.call(arguments, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4239,7 +4231,7 @@
|
||||
// (in V8 `Function#bind` is slower except when partially applied)
|
||||
return support.fastBind || (nativeBind && arguments.length > 2)
|
||||
? nativeBind.call.apply(nativeBind, arguments)
|
||||
: createBound(func, thisArg, slice(arguments, 2));
|
||||
: createBound(func, thisArg, nativeSlice.call(arguments, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4312,7 +4304,7 @@
|
||||
* // => 'hi, moe!'
|
||||
*/
|
||||
function bindKey(object, key) {
|
||||
return createBound(object, key, slice(arguments, 2), indicatorObject);
|
||||
return createBound(object, key, nativeSlice.call(arguments, 2), indicatorObject);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4511,7 +4503,7 @@
|
||||
* // returns from the function before `alert` is called
|
||||
*/
|
||||
function defer(func) {
|
||||
var args = slice(arguments, 1);
|
||||
var args = nativeSlice.call(arguments, 1);
|
||||
return setTimeout(function() { func.apply(undefined, args); }, 1);
|
||||
}
|
||||
// use `setImmediate` if it's available in Node.js
|
||||
@@ -4537,7 +4529,7 @@
|
||||
* // => 'logged later' (Appears after one second.)
|
||||
*/
|
||||
function delay(func, wait) {
|
||||
var args = slice(arguments, 2);
|
||||
var args = nativeSlice.call(arguments, 2);
|
||||
return setTimeout(function() { func.apply(undefined, args); }, wait);
|
||||
}
|
||||
|
||||
@@ -4623,7 +4615,7 @@
|
||||
* // => 'hi moe'
|
||||
*/
|
||||
function partial(func) {
|
||||
return createBound(func, slice(arguments, 1));
|
||||
return createBound(func, nativeSlice.call(arguments, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4654,7 +4646,7 @@
|
||||
* // => { '_': _, 'jq': $ }
|
||||
*/
|
||||
function partialRight(func) {
|
||||
return createBound(func, slice(arguments, 1), null, indicatorObject);
|
||||
return createBound(func, nativeSlice.call(arguments, 1), null, indicatorObject);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user