From 5adb4ee95ca381422c8920c072836453ab0c5c94 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 2 Feb 2018 17:55:06 -0800 Subject: [PATCH] Make _.defaults avoid accessing property values it doesn't need to. [closes #2983] --- lodash.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lodash.js b/lodash.js index 04d9ffd80..932c1671f 100644 --- a/lodash.js +++ b/lodash.js @@ -12774,9 +12774,35 @@ * _.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); * // => { 'a': 1, 'b': 2 } */ - var defaults = baseRest(function(args) { - args.push(undefined, customDefaultsAssignIn); - return apply(assignInWith, undefined, args); + var defaults = baseRest(function(object, sources) { + object = Object(object); + + var index = -1; + var length = sources.length; + var guard = length > 2 ? sources[2] : undefined; + + if (guard && isIterateeCall(sources[0], sources[1], guard)) { + length = 1; + } + + while (++index < length) { + var source = sources[index]; + var props = keysIn(source); + var propsIndex = -1; + var propsLength = props.length; + + while (++propsIndex < propsLength) { + var key = props[propsIndex]; + var value = object[key]; + + if (value === undefined || + (eq(value, objectProto[key]) && !hasOwnProperty.call(object, key))) { + object[key] = source[key]; + } + } + } + + return object; }); /**