Optimize _.invoke.

This commit is contained in:
jdalton
2015-04-14 12:29:19 -07:00
parent 75445b8b98
commit 637a6b5f7e

View File

@@ -4272,7 +4272,7 @@
* @returns {*} Returns the result of the invoked method. * @returns {*} Returns the result of the invoked method.
*/ */
function invokePath(object, path, args) { function invokePath(object, path, args) {
if (!isKey(path, object)) { if (object != null && !isKey(path, object)) {
path = toPath(path); path = toPath(path);
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1)); object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
path = last(path); path = last(path);
@@ -6816,11 +6816,13 @@
var invoke = restParam(function(collection, path, args) { var invoke = restParam(function(collection, path, args) {
var index = -1, var index = -1,
isFunc = typeof path == 'function', isFunc = typeof path == 'function',
isProp = isKey(path),
length = getLength(collection), length = getLength(collection),
result = isLength(length) ? Array(length) : []; result = isLength(length) ? Array(length) : [];
baseEach(collection, function(value) { baseEach(collection, function(value) {
result[++index] = isFunc ? path.apply(value, args) : invokePath(value, path, args); var func = isFunc ? path : (isProp && value != null && value[path]);
result[++index] = func ? func.apply(value, args) : invokePath(value, path, args);
}); });
return result; return result;
}); });
@@ -10019,7 +10021,7 @@
* // => 'default' * // => 'default'
*/ */
function result(object, path, defaultValue) { function result(object, path, defaultValue) {
if (!isKey(path, object)) { if (object != null && !isKey(path, object)) {
path = toPath(path); path = toPath(path);
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1)); object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
path = last(path); path = last(path);