How Lodash pickBy Works Internally
Lodash’s _.pickBy method creates an object composed of
filtered properties by converting target keys into an array, executing
an iteratee predicate against each property, and assembling a new object
from the truthy results. This article explores the internal mechanics of
_.pickBy, covering how it leverages native array traversal,
evaluates mapped boolean expressions, and safely constructs the
output.
The Entry Point and Delegation
When _.pickBy(object, predicate) is invoked, Lodash
first normalizes the parameters. If the target object is
null or undefined, the method immediately
returns an empty object {}. If a predicate function is
omitted, it defaults to _.identity, which evaluates the
truthiness of the values themselves.
Lodash delegates the actual selection logic to an internal function
named basePickBy. Before iterating, basePickBy
must determine which properties to inspect.
Extracting Property Keys into Arrays
JavaScript objects do not feature native array iteration methods such
as .filter() or .forEach(). To achieve
high-performance traversal, Lodash converts the object’s properties into
a linear array of keys using getAllKeysIn (or
keysIn).
This extraction gathers:
- Enumerable own string properties.
- Inherited enumerable properties from the prototype chain.
- Enumerable symbol properties (if supported in the runtime environment).
By flattening the object's keys into a contiguous array, Lodash can
bypass the overhead of standard for...in loops and rely on
predictable array indexing.
Iteration via
basePickBy
Once the keys array is generated, basePickBy executes
using three primary arguments: the source object, the
props array, and the predicate.
The function initializes an empty result object and enters a
high-speed while loop:
function basePickBy(object, props, predicate) {
var index = -1,
length = props.length,
result = {};
while (++index < length) {
var key = props[index],
value = object[key];
if (predicate(value, key)) {
baseAssignValue(result, key, value);
}
}
return result;
}Boolean Evaluation and Property Assignment
During each step of the array loop:
- Accessing Members: The current key is retrieved
using native array indexing (
props[index]), and its associated value is accessed from the source object (object[key]). - Predicate Execution: Lodash invokes
predicate(value, key). The predicate acts as a boolean mapper. - Truthy Verification: The return value is evaluated
in a native JavaScript conditional
ifstatement. If the iteratee returns a truthy value, the condition passes. - Assignment: Instead of standard property assignment
(
result[key] = value), Lodash calls an internal utility namedbaseAssignValue. This helper safely defines the property on theresultobject, protecting against prototype pollution vulnerabilities (such as overwriting__proto__) and preventing custom setters on the prototype from executing.
Once the index reaches the array length, the iteration terminates, and Lodash returns the newly populated object containing only the properties that passed the boolean check.