Refactor reduceRight and modify a _.difference benchmark.

Former-commit-id: b70272ac5316fe1bee52b9611a1a5ea4d761dd3c
This commit is contained in:
John-David Dalton
2012-09-22 15:55:57 -07:00
parent 1ca26ce676
commit 8532dc4b75
3 changed files with 16 additions and 16 deletions

View File

@@ -133,7 +133,7 @@
'random': [], 'random': [],
'range': [], 'range': [],
'reduce': [], 'reduce': [],
'reduceRight': ['keys', 'reduce'], 'reduceRight': ['forEach', 'keys'],
'reject': ['identity'], 'reject': ['identity'],
'rest': [], 'rest': [],
'result': ['isFunction'], 'result': ['isFunction'],
@@ -1146,7 +1146,7 @@
source = source.replace(/noArraySliceOnStrings *\?[^:]+: *([^)]+)/g, '$1'); source = source.replace(/noArraySliceOnStrings *\?[^:]+: *([^)]+)/g, '$1');
// remove `noCharByIndex` from `_.reduceRight` // remove `noCharByIndex` from `_.reduceRight`
source = source.replace(/noCharByIndex *&&[^:]+: *([^;]+)/g, '$1'); source = source.replace(/}\s*else if *\(noCharByIndex[^}]+/, '');
source = removeVar(source, 'extendIteratorOptions'); source = removeVar(source, 'extendIteratorOptions');
source = removeVar(source, 'iteratorTemplate'); source = removeVar(source, 'iteratorTemplate');

View File

@@ -2220,19 +2220,23 @@
if (!collection) { if (!collection) {
return accumulator; return accumulator;
} }
var length = collection.length, var iteratee = collection,
length = collection.length,
noaccum = arguments.length < 3; noaccum = arguments.length < 3;
if (length !== +length) { if (length !== +length) {
var props = keys(collection); var props = keys(collection);
length = props.length; length = props.length;
} else if (noCharByIndex && toString.call(collection) == stringClass) {
iteratee = collection.split('');
} }
return reduce(collection, function(accumulator, value, index, object) { forEach(collection, function(value, index, object) {
var index = props ? props[--length] : --length; index = props ? props[--length] : --length;
return noaccum accumulator = noaccum
? (noaccum = false, object[index]) ? (noaccum = false, iteratee[index])
: callback.call(thisArg, accumulator, object[index], index, object); : callback.call(thisArg, accumulator, iteratee[index], index, object);
}, accumulator); });
return accumulator;
} }
/** /**

View File

@@ -276,7 +276,6 @@
var twentyFiveValues = Array(25),\ var twentyFiveValues = Array(25),\
twentyFiveValues2 = Array(25),\ twentyFiveValues2 = Array(25),\
fiftyValues = Array(50),\ fiftyValues = Array(50),\
fiftyValues2 = Array(50),\
seventyFiveValues = Array(75),\ seventyFiveValues = Array(75),\
seventyFiveValues2 = Array(75),\ seventyFiveValues2 = Array(75),\
lowerChars = "abcdefghijklmnopqrstuvwxyz".split(""),\ lowerChars = "abcdefghijklmnopqrstuvwxyz".split(""),\
@@ -294,14 +293,11 @@
}\ }\
fiftyValues[index] =\ fiftyValues[index] =\
seventyFiveValues[index] = lowerChars[index];\ seventyFiveValues[index] = lowerChars[index];\
\
fiftyValues2[index] =\
seventyFiveValues2[index] = upperChars[index];\ seventyFiveValues2[index] = upperChars[index];\
}\ }\
else {\ else {\
if (index < 50) {\ if (index < 50) {\
fiftyValues[index] = index;\ fiftyValues[index] = index;\
fiftyValues2[index] = index + (index < 40 ? 75 : 0);\
}\ }\
seventyFiveValues[index] = index;\ seventyFiveValues[index] = index;\
seventyFiveValues2[index] = index + (index < 60 ? 75 : 0);\ seventyFiveValues2[index] = index + (index < 60 ? 75 : 0);\
@@ -575,13 +571,13 @@
); );
suites.push( suites.push(
Benchmark.Suite('`_.difference` iterating 50 elements') Benchmark.Suite('`_.difference` iterating 50 and 75 elements')
.add('Lo-Dash', { .add('Lo-Dash', {
'fn': 'lodash.difference(fiftyValues, fiftyValues2)', 'fn': 'lodash.difference(fiftyValues, seventyFiveValues2)',
'teardown': 'function multiArrays(){}' 'teardown': 'function multiArrays(){}'
}) })
.add('Underscore', { .add('Underscore', {
'fn': '_.difference(fiftyValues, fiftyValues2)', 'fn': '_.difference(fiftyValues, seventyFiveValues2)',
'teardown': 'function multiArrays(){}' 'teardown': 'function multiArrays(){}'
}) })
); );