mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 03:47:50 +00:00
Optimize _.reduce and _.reduceRight.
This commit is contained in:
55
lodash.js
55
lodash.js
@@ -1179,6 +1179,30 @@
|
|||||||
return accumulator;
|
return accumulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A specialized version of `_.reduceRight` for arrays without support for
|
||||||
|
* callback shorthands or `this` binding.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Array} array The array to iterate over.
|
||||||
|
* @param {Function} iterator The function called per iteration.
|
||||||
|
* @param {*} [accumulator] The initial value.
|
||||||
|
* @param {boolean} [initFromArray=false] Specify using the first element of
|
||||||
|
* `array` as the initial value.
|
||||||
|
* @returns {*} Returns the accumulated value.
|
||||||
|
*/
|
||||||
|
function arrayReduceRight(array, iterator, accumulator, initFromArray) {
|
||||||
|
var length = array.length;
|
||||||
|
|
||||||
|
if (initFromArray && length) {
|
||||||
|
accumulator = array[--length];
|
||||||
|
}
|
||||||
|
while (length--) {
|
||||||
|
accumulator = iterator(accumulator, array[length], length, array);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A specialized version of `_.some` for arrays without support for callback
|
* A specialized version of `_.some` for arrays without support for callback
|
||||||
* shorthands or `this` binding.
|
* shorthands or `this` binding.
|
||||||
@@ -1676,7 +1700,7 @@
|
|||||||
* @private
|
* @private
|
||||||
* @param {Array|Object|string} collection The collection to search.
|
* @param {Array|Object|string} collection The collection to search.
|
||||||
* @param {Function} predicate The function called per iteration.
|
* @param {Function} predicate The function called per iteration.
|
||||||
* @param {Function} eachFunc The function to iterate over the collection.
|
* @param {Function} eachFunc The function to iterate over `collection`.
|
||||||
* @param {boolean} [retKey=false] Specify returning the key of the found
|
* @param {boolean} [retKey=false] Specify returning the key of the found
|
||||||
* element instead of the element itself.
|
* element instead of the element itself.
|
||||||
* @returns {*} Returns the found element or its key, else `undefined`.
|
* @returns {*} Returns the found element or its key, else `undefined`.
|
||||||
@@ -2239,19 +2263,21 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.reduce` without support for callback
|
* The base implementation of `_.reduce` and `_.reduceRight` without support
|
||||||
* shorthands or `this` binding.
|
* for callback shorthands or `this` binding, which iterates over `collection`
|
||||||
|
* usingthe provided `eachFunc`.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Array|Object|string} collection The collection to iterate over.
|
* @param {Array|Object|string} collection The collection to iterate over.
|
||||||
* @param {Function} iterator The function called per iteration.
|
* @param {Function} iterator The function called per iteration.
|
||||||
* @param {*} [accumulator] The initial value.
|
* @param {*} accumulator The initial value.
|
||||||
* @param {boolean} [initFromCollection=false] Specify using the first element
|
* @param {boolean} initFromCollection Specify using the first element
|
||||||
* of `collection` as the initial value.
|
* of `collection` as the initial value.
|
||||||
|
* @param {Function} eachFunc The function to iterate over `collection`.
|
||||||
* @returns {*} Returns the accumulated value.
|
* @returns {*} Returns the accumulated value.
|
||||||
*/
|
*/
|
||||||
function baseReduce(collection, iterator, accumulator, initFromCollection) {
|
function baseReduce(collection, iterator, accumulator, initFromCollection, eachFunc) {
|
||||||
baseEach(collection, function(value, index, collection) {
|
eachFunc(collection, function(value, index, collection) {
|
||||||
accumulator = initFromCollection
|
accumulator = initFromCollection
|
||||||
? (initFromCollection = false, value)
|
? (initFromCollection = false, value)
|
||||||
: iterator(accumulator, value, index, collection)
|
: iterator(accumulator, value, index, collection)
|
||||||
@@ -5066,10 +5092,8 @@
|
|||||||
* // => { 'a': 3, 'b': 6, 'c': 9 }
|
* // => { 'a': 3, 'b': 6, 'c': 9 }
|
||||||
*/
|
*/
|
||||||
function reduce(collection, iterator, accumulator, thisArg) {
|
function reduce(collection, iterator, accumulator, thisArg) {
|
||||||
iterator = lodash.callback(iterator, thisArg, 4);
|
|
||||||
|
|
||||||
var func = isArray(collection) ? arrayReduce : baseReduce;
|
var func = isArray(collection) ? arrayReduce : baseReduce;
|
||||||
return func(collection, iterator, accumulator, arguments.length < 3);
|
return func(collection, lodash.callback(iterator, thisArg, 4), accumulator, arguments.length < 3, baseEach);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5092,15 +5116,8 @@
|
|||||||
* // => [4, 5, 2, 3, 0, 1]
|
* // => [4, 5, 2, 3, 0, 1]
|
||||||
*/
|
*/
|
||||||
function reduceRight(collection, iterator, accumulator, thisArg) {
|
function reduceRight(collection, iterator, accumulator, thisArg) {
|
||||||
var initFromCollection = arguments.length < 3;
|
var func = isArray(collection) ? arrayReduceRight : baseReduce;
|
||||||
iterator = lodash.callback(iterator, thisArg, 4);
|
return func(collection, lodash.callback(iterator, thisArg, 4), accumulator, arguments.length < 3, baseEachRight);
|
||||||
|
|
||||||
baseEachRight(collection, function(value, index, collection) {
|
|
||||||
accumulator = initFromCollection
|
|
||||||
? (initFromCollection = false, value)
|
|
||||||
: iterator(accumulator, value, index, collection);
|
|
||||||
});
|
|
||||||
return accumulator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user