Add falsey value optimizations for baseDifference, baseFlatten, baseUniq, _.flatten, and _.uniq.

This commit is contained in:
John-David Dalton
2014-02-22 17:10:34 -08:00
parent d4fac798fc
commit c2b1d3b551

View File

@@ -1288,9 +1288,12 @@
* @returns {Array} Returns a new array of filtered values. * @returns {Array} Returns a new array of filtered values.
*/ */
function baseDifference(array, values) { function baseDifference(array, values) {
var length = array ? array.length : 0;
if (!length) {
return [];
}
var index = -1, var index = -1,
indexOf = getIndexOf(), indexOf = getIndexOf(),
length = array ? array.length : 0,
result = []; result = [];
if (createCache && values && indexOf === baseIndexOf && values.length >= LARGE_ARRAY_SIZE) { if (createCache && values && indexOf === baseIndexOf && values.length >= LARGE_ARRAY_SIZE) {
@@ -1702,9 +1705,12 @@
* @returns {Array} Returns a duplicate-value-free array. * @returns {Array} Returns a duplicate-value-free array.
*/ */
function baseUniq(array, isSorted, callback) { function baseUniq(array, isSorted, callback) {
var length = array ? array.length : 0;
if (!length) {
return [];
}
var index = -1, var index = -1,
indexOf = getIndexOf(), indexOf = getIndexOf(),
length = array ? array.length : 0,
isLarge = createCache && !isSorted && indexOf === baseIndexOf && length >= LARGE_ARRAY_SIZE, isLarge = createCache && !isSorted && indexOf === baseIndexOf && length >= LARGE_ARRAY_SIZE,
result = []; result = [];
@@ -2412,9 +2418,12 @@
* // => ['hoppy', 'baby puss', 'dino'] * // => ['hoppy', 'baby puss', 'dino']
*/ */
function flatten(array, isShallow, callback, thisArg) { function flatten(array, isShallow, callback, thisArg) {
var type = typeof isShallow; var length = array ? array.length : 0;
if (!length) {
return [];
}
// juggle arguments // juggle arguments
var type = typeof isShallow;
if (type != 'boolean' && isShallow != null) { if (type != 'boolean' && isShallow != null) {
thisArg = callback; thisArg = callback;
callback = isShallow; callback = isShallow;
@@ -3090,9 +3099,12 @@
* // => [{ 'x': 1 }, { 'x': 2 }] * // => [{ 'x': 1 }, { 'x': 2 }]
*/ */
function uniq(array, isSorted, callback, thisArg) { function uniq(array, isSorted, callback, thisArg) {
var type = typeof isSorted; var length = array ? array.length : 0;
if (!length) {
return [];
}
// juggle arguments // juggle arguments
var type = typeof isSorted;
if (type != 'boolean' && isSorted != null) { if (type != 'boolean' && isSorted != null) {
thisArg = callback; thisArg = callback;
callback = isSorted; callback = isSorted;