Skip to content

Commit

Permalink
Simplify reduce wrapper to just determine initial
Browse files Browse the repository at this point in the history
Well, and optimize `iteratee`, cause we don't really need context
anywhere else.
  • Loading branch information
jridgewell committed May 8, 2015
1 parent c474aa3 commit a7f4623
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@
var createReduce = function(dir) {
// Optimized iterator function as using arguments.length
// in the main function will deoptimize the, see #1991.
var iterator = function(obj, iteratee, memo, keys, index, length) {
var reducer = function(obj, iteratee, memo, initial) {
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
index = dir > 0 ? 0 : length - 1;
if (!initial) {
memo = obj[keys ? keys[index] : index];
index += dir;
}
for (; index >= 0 && index < length; index += dir) {
var currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
Expand All @@ -192,16 +199,8 @@
};

return function(obj, iteratee, memo, context) {
iteratee = optimizeCb(iteratee, context, 4);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
index = dir > 0 ? 0 : length - 1;
// Determine the initial value if none is provided.
if (arguments.length < 3) {
memo = obj[keys ? keys[index] : index];
index += dir;
}
return iterator(obj, iteratee, memo, keys, index, length);
var initial = arguments.length >= 3;
return reducer(obj, optimizeCb(iteratee, context, 4), memo, initial);
};
};

Expand Down

0 comments on commit a7f4623

Please sign in to comment.