From b535c4f32110c9bbd4ec97d9bb0ee06a6acd6cd1 Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 11 Mar 2015 00:42:31 -0700 Subject: [PATCH] Optimize `_.reduce` and `_.reduceRight`. --- lodash.src.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 617dbb345..1a1b2aab0 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -6641,8 +6641,10 @@ * // => { 'a': 3, 'b': 6 } (iteration order is not guaranteed) */ function reduce(collection, iteratee, accumulator, thisArg) { - var func = isArray(collection) ? arrayReduce : baseReduce; - return func(collection, getCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEach); + var initFromArray = arguments.length < 3; + return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection)) + ? arrayReduce(collection, iteratee, accumulator, initFromArray) + : baseReduce(collection, getCallback(iteratee, thisArg, 4), accumulator, initFromArray, baseEach); } /** @@ -6668,8 +6670,10 @@ * // => [4, 5, 2, 3, 0, 1] */ function reduceRight(collection, iteratee, accumulator, thisArg) { - var func = isArray(collection) ? arrayReduceRight : baseReduce; - return func(collection, getCallback(iteratee, thisArg, 4), accumulator, arguments.length < 3, baseEachRight); + var initFromArray = arguments.length < 3; + return (typeof iteratee == 'function' && typeof thisArg == 'undefined' && isArray(collection)) + ? arrayReduceRight(collection, iteratee, accumulator, initFromArray) + : baseReduce(collection, getCallback(iteratee, thisArg, 4), accumulator, initFromArray, baseEachRight); } /**