Editing Summing per type This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"> </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) // set up a 1 million item array var bigData = [], types = "ABCDEF".split(''), typesLength = types.length; _(1000000).times(function(i) { bigData.push({ type: types[_.random(0, typesLength - 1)], val: _.random(1, 10) }); }); Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var totalPerType = {}; for (var i = 0, len = bigData.length; i < len; ++i) { totalPerType[bigData[i].type] = totalPerType[bigData[i].type] || 0; totalPerType[bigData[i].type] += bigData[i].val; } var out = _.map(totalPerType, function(sum, type) { return { 'type': type, 'total': sum }; }); Test 2 Title Async (check if this is an asynchronous test) Code var out = []; for (var i = 0, len = bigData.length; i < len; ++i) { var totalObj = _.find(out, function(x) { return x.type == bigData[i].type; }); if (!totalObj) out.push({ 'type': bigData[i].type, 'total': bigData[i].val }); else totalObj.total += bigData[i].val; } Test 3 Title Async (check if this is an asynchronous test) Code var summed_by_type = _(bigData).reduce(function(mem, d) { mem[d.type] = (mem[d.type] || 0) + d.val return mem }, {}); out = _(summed_by_type).map(function(v, k) { return { type: k, total: v } }); Test 4 Title Async (check if this is an asynchronous test) Code var groups = _(bigData).groupBy('type'); var out = _(groups).map(function(g, key) { return { type: key, val: _(g).reduce(function(m,x) { return m + x.val; }, 0) }; }); Test 5 Title Async (check if this is an asynchronous test) Code var totalPerType = {}; for (var i = 0, len = bigData.length; i < len; ++i) { totalPerType[bigData[i].type] += (totalPerType[bigData[i].type] || 0) + bigData[i].val; } var out = _.map(totalPerType, function(sum, type) { return { 'type': type, 'total': sum }; });