Optimize _.invoke for arrays.

This commit is contained in:
John-David Dalton
2014-02-02 19:05:34 -08:00
parent 81a859604c
commit 2543afebf1
9 changed files with 232 additions and 193 deletions

View File

@@ -2481,15 +2481,22 @@
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
function invoke(collection, methodName) {
var args = slice(arguments, 2),
index = -1,
var index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
if (arguments.length < 3 && typeof length == 'number') {
while (++index < length) {
var value = collection[index];
result[index] = isFunc ? methodName.call(value) : value[methodName]();
}
} else {
var args = slice(arguments, 2);
baseEach(collection, function(value) {
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
});
}
return result;
}