(function() {
"use strict";
/* reduce_.reduce(list, iteratee, [memo], [context]) Aliases: inject, foldl
Also known as inject and foldl, reduce boils down a list of values into a single value. Memo is the initial state of the reduction, and each successive step of it should be returned by iteratee. The iteratee is passed four arguments: the memo, then the value and index (or key) of the iteration, and finally a reference to the entire list.
If no memo is passed to the initial invocation of reduce, the iteratee is not invoked on the first element of the list. The first element is instead passed as the memo in the invocation of the iteratee on the next element in the list. */
var numbers = [1,2,3,4,5];
var t0 = performance.now();
var value = _.reduce(numbers, function(lastReducedValue, item) {
return lastReducedValue + item;
});
var t1 = performance.now();
console.log("Call to _.reduce took " + (t1 - t0) + " milliseconds.")
console.log ("\tusing _.reduce(numbers, function(lastReducedValue, item)...", numbers);
console.log ("\tThe reduced value is: ", value);
var t2 = performance.now();
for (let i = j = 0; i < numbers.length; i++) {
j += numbers[i];
value = j;
}
var t3 = performance.now();
console.log("Call to forLoop accumulation took " + (t3 - t2) + " milliseconds.")
console.log ("\tThe reduced value is: ", value);
})();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.