How Lodash maxBy Finds Deeply Nested Extremes

Lodash's _.maxBy method provides a reliable mechanism for finding the maximum element in a collection based on deep property paths or transformation functions. This article explores the internal architecture behind _.maxBy, detailing how its foundational loop function, baseExtremum, uses a dual-state accumulator strategy to track both computed criteria and source references, while safely resolving deeply nested object values without performance overhead or loss of context.

The Foundation: baseExtremum

At the heart of _.maxBy lies an internal helper function called baseExtremum. When _.maxBy(array, iteratee) is invoked, it delegates immediately to:

baseExtremum(array, getIteratee(iteratee, 2), baseGt);

Here, baseGt serves as the comparison operator (>), and getIteratee normalizes strings, property paths, or custom functions into an executable accessor. Rather than performing a two-pass map-then-reduce operation—which would generate intermediate arrays and increase memory footprint—baseExtremum performs a single linear sweep (O(n)) using an internal accumulator design.

The Dual-State Accumulator Strategy

To preserve the identity of the original collection item while comparing transformed or nested criteria, baseExtremum tracks two distinct accumulator values across the iteration:

  1. The Result Reference (result): Holds the reference to the actual item inside the input array that currently possesses the highest evaluated value.
  2. The Computed Metric (computed): Holds the derived primitive value produced by applying the iteratee to the current item.

During each iteration, the loop applies the iteratee to the current element to obtain a candidate value. It then compares this candidate against computed using the comparator:

for (const value of array) {
  const current = iteratee(value);
  if (current != null && (computed === undefined
        ? (current === current && !isSymbol(current))
        : comparator(current, computed)
      )) {
    computed = current;
    result = value;
  }
}
return result;

This separation avoids re-evaluating the iteratee on previously visited elements while ensuring that the return value remains the intact object, regardless of how deeply nested the target comparison key was.

Resolving Deep Paths via baseIteratee

When developers pass a nested path string (such as 'user.analytics.score') into _.maxBy, Lodash compiles this string using baseIteratee and property.

Under the hood, this relies on baseGet, which navigates the object tree segment by segment. If any intermediate key in the path is null or undefined, the traversal short-circuits gracefully and yields undefined instead of throwing a TypeError.

Because the dual accumulator evaluates the iteratee per element, baseGet extracts the deeply nested numerical or comparable value directly into the current slot, while the top-level parent object remains tethered to result.

Guarding Against Non-Comparable Values

A critical aspect of Lodash's accumulator strategy is its strictness around invalid comparisons:

By pairing single-pass dual accumulation with defensive property extraction, _.maxBy maps and resolves deeply nested criteria while preserving the complete source object.