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': [],
'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');

View File

@@ -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;
}
/**

View File

@@ -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(){}'
})
);