From 8532dc4b756e8e1a7228b0a969bc54db1f4f56a5 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 22 Sep 2012 15:55:57 -0700 Subject: [PATCH] Refactor `reduceRight` and modify a `_.difference` benchmark. Former-commit-id: b70272ac5316fe1bee52b9611a1a5ea4d761dd3c --- build.js | 4 ++-- lodash.js | 18 +++++++++++------- perf/perf.js | 10 +++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/build.js b/build.js index bb195371c..12df68099 100755 --- a/build.js +++ b/build.js @@ -133,7 +133,7 @@ 'random': [], 'range': [], 'reduce': [], - 'reduceRight': ['keys', 'reduce'], + 'reduceRight': ['forEach', 'keys'], 'reject': ['identity'], 'rest': [], 'result': ['isFunction'], @@ -1146,7 +1146,7 @@ source = source.replace(/noArraySliceOnStrings *\?[^:]+: *([^)]+)/g, '$1'); // remove `noCharByIndex` from `_.reduceRight` - source = source.replace(/noCharByIndex *&&[^:]+: *([^;]+)/g, '$1'); + source = source.replace(/}\s*else if *\(noCharByIndex[^}]+/, ''); source = removeVar(source, 'extendIteratorOptions'); source = removeVar(source, 'iteratorTemplate'); diff --git a/lodash.js b/lodash.js index b1811a625..a2984ada9 100644 --- a/lodash.js +++ b/lodash.js @@ -2220,19 +2220,23 @@ if (!collection) { return accumulator; } - var length = collection.length, + var iteratee = collection, + length = collection.length, noaccum = arguments.length < 3; if (length !== +length) { var props = keys(collection); length = props.length; + } else if (noCharByIndex && toString.call(collection) == stringClass) { + iteratee = collection.split(''); } - return reduce(collection, function(accumulator, value, index, object) { - var index = props ? props[--length] : --length; - return noaccum - ? (noaccum = false, object[index]) - : callback.call(thisArg, accumulator, object[index], index, object); - }, accumulator); + forEach(collection, function(value, index, object) { + index = props ? props[--length] : --length; + accumulator = noaccum + ? (noaccum = false, iteratee[index]) + : callback.call(thisArg, accumulator, iteratee[index], index, object); + }); + return accumulator; } /** diff --git a/perf/perf.js b/perf/perf.js index 51e8ea1d0..399548cb9 100644 --- a/perf/perf.js +++ b/perf/perf.js @@ -276,7 +276,6 @@ var twentyFiveValues = Array(25),\ twentyFiveValues2 = Array(25),\ fiftyValues = Array(50),\ - fiftyValues2 = Array(50),\ seventyFiveValues = Array(75),\ seventyFiveValues2 = Array(75),\ lowerChars = "abcdefghijklmnopqrstuvwxyz".split(""),\ @@ -294,14 +293,11 @@ }\ fiftyValues[index] =\ seventyFiveValues[index] = lowerChars[index];\ - \ - fiftyValues2[index] =\ seventyFiveValues2[index] = upperChars[index];\ }\ else {\ if (index < 50) {\ fiftyValues[index] = index;\ - fiftyValues2[index] = index + (index < 40 ? 75 : 0);\ }\ seventyFiveValues[index] = index;\ seventyFiveValues2[index] = index + (index < 60 ? 75 : 0);\ @@ -575,13 +571,13 @@ ); suites.push( - Benchmark.Suite('`_.difference` iterating 50 elements') + Benchmark.Suite('`_.difference` iterating 50 and 75 elements') .add('Lo-Dash', { - 'fn': 'lodash.difference(fiftyValues, fiftyValues2)', + 'fn': 'lodash.difference(fiftyValues, seventyFiveValues2)', 'teardown': 'function multiArrays(){}' }) .add('Underscore', { - 'fn': '_.difference(fiftyValues, fiftyValues2)', + 'fn': '_.difference(fiftyValues, seventyFiveValues2)', 'teardown': 'function multiArrays(){}' }) );