Permalink
Browse files

Use `nativeSlice` when possible and adjust `largeArraySize` to accoun…

…t for the recent `cachedContains` tweaks.
  • Loading branch information...
1 parent 81ebaa8 commit 86fbeab81dcbf873d79b96a980a2f4535ee2f8e4 @jdalton jdalton committed Apr 6, 2013
View
@@ -175,7 +175,7 @@
'value': ['forOwn', 'isArray'],
'values': ['keys'],
'where': ['filter'],
- 'without': ['indexOf'],
+ 'without': ['difference'],
'wrap': [],
'zip': ['max', 'pluck'],
'zipObject': [],
@@ -2392,23 +2392,6 @@
'}'
].join('\n'));
- // replace `_.without`
- source = replaceFunction(source, 'without', [
- 'function without(array) {',
- ' var index = -1,',
- ' length = array.length,',
- ' result = [];',
- '',
- ' while (++index < length) {',
- ' var value = array[index];',
- ' if (indexOf(arguments, value, 1) < 0) {',
- ' result.push(value);',
- ' }',
- ' }',
- ' return result',
- '}'
- ].join('\n'));
-
// add `_.findWhere`
source = source.replace(matchFunction(source, 'find'), function(match) {
var indent = getIndent(match);
@@ -2446,10 +2429,9 @@
});
});
- // replace `slice` with `slice.call`
+ // replace `slice` with `nativeSlice.call`
source = removeFunction(source, 'slice');
- source = source.replace(/^(( *)setTimeout = context.setTimeout)([,;])/m, '$1,\n$2slice = arrayRef.slice$3');
- source = source.replace(/([^.]\bslice)\(/g, '$1.call(');
+ source = source.replace(/([^.])\bslice\(/g, '$1nativeSlice.call(');
// replace `lodash.createCallback` references with `createCallback`
if (!exposeCreateCallback) {
@@ -33,6 +33,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,
@@ -121,7 +124,6 @@
hasOwnProperty = objectRef.hasOwnProperty,
push = arrayRef.push,
setTimeout = window.setTimeout,
- slice = arrayRef.slice,
toString = objectRef.toString;
/* Native method shortcuts for methods with the same name as other `lodash` methods */
@@ -132,7 +134,8 @@
nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
nativeMax = Math.max,
nativeMin = Math.min,
- nativeRandom = Math.random;
+ nativeRandom = Math.random,
+ nativeSlice = arrayRef.slice;
/** Detect various environments */
var isIeOpera = reNative.test(window.attachEvent),
@@ -574,7 +577,7 @@
*/
function clone(value) {
return isObject(value)
- ? (isArray(value) ? slice.call(value) : assign({}, value))
+ ? (isArray(value) ? nativeSlice.call(value) : assign({}, value))
: value;
}
@@ -1534,7 +1537,7 @@
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
function invoke(collection, methodName) {
- var args = slice.call(arguments, 2),
+ var args = nativeSlice.call(arguments, 2),
index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
@@ -2064,7 +2067,7 @@
*/
function toArray(collection) {
if (isArray(collection)) {
- return slice.call(collection);
+ return nativeSlice.call(collection);
}
if (collection && typeof collection.length == 'number') {
return map(collection);
@@ -2075,6 +2078,37 @@
/*--------------------------------------------------------------------------*/
/**
+ * Creates an array of `array` elements not present in the other arrays
+ * using strict equality for comparisons, i.e. `===`.
+ *
+ * @static
+ * @memberOf _
+ * @category Arrays
+ * @param {Array} array The array to process.
+ * @param {Array} [array1, array2, ...] Arrays to check.
+ * @returns {Array} Returns a new array of `array` elements not present in the
+ * other arrays.
+ * @example
+ *
+ * _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
+ * // => [1, 3, 4]
+ */
+ function difference(array) {
+ var index = -1,
+ length = array.length,
+ flattened = concat.apply(arrayRef, arguments),
+ result = [];
+
+ while (++index < length) {
+ var value = array[index];
+ if (indexOf(flattened, value, length) < 0) {
+ result.push(value);
+ }
+ }
+ return result;
+ }
+
+ /**
* Gets the first element of the `array`. If a number `n` is passed, the first
* `n` elements of the `array` are returned. If a `callback` function is passed,
* elements at the beginning of the array are returned as long as the `callback`
@@ -2148,7 +2182,7 @@
return array[0];
}
}
- return slice.call(array, 0, nativeMin(nativeMax(0, n), length));
+ return nativeSlice.call(array, 0, nativeMin(nativeMax(0, n), length));
}
}
@@ -2266,7 +2300,7 @@
} else {
n = (callback == null || thisArg) ? 1 : callback || n;
}
- return slice.call(array, 0, nativeMin(nativeMax(0, length - n), length));
+ return nativeSlice.call(array, 0, nativeMin(nativeMax(0, length - n), length));
}
/**
@@ -2343,7 +2377,7 @@
return array[length - 1];
}
}
- return slice.call(array, nativeMax(0, length - n));
+ return nativeSlice.call(array, nativeMax(0, length - n));
}
}
@@ -2451,7 +2485,7 @@
} else {
n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
}
- return slice.call(array, n);
+ return nativeSlice.call(array, n);
}
/**
@@ -2535,17 +2569,7 @@
* // => [2, 3, 4]
*/
function without(array) {
- var index = -1,
- length = array.length,
- result = [];
-
- while (++index < length) {
- var value = array[index];
- if (indexOf(arguments, value, 1) < 0) {
- result.push(value);
- }
- }
- return result
+ return difference(array, nativeSlice.call(arguments, 1));
}
/*--------------------------------------------------------------------------*/
@@ -2577,7 +2601,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.call(arguments, 2));
+ : createBound(func, thisArg, nativeSlice.call(arguments, 2));
}
/**
@@ -2961,6 +2985,7 @@
lodash.bindAll = bindAll;
lodash.countBy = countBy;
lodash.defaults = defaults;
+ lodash.difference = difference;
lodash.filter = filter;
lodash.forEach = forEach;
lodash.functions = functions;
Oops, something went wrong.

0 comments on commit 86fbeab

Please sign in to comment.