How Lodash Throttle Handles Trailing Edge Calls
Lodash’s _.throttle limits how frequently a function can
run over time while ensuring that bursts of rapid calls are resolved
cleanly. By default, it enables both leading and trailing executions,
guaranteeing that the final function call in an event stream is not
lost. This article explains the internal mechanics of the trailing edge
in _.throttle, detailing how it retains the latest
arguments, manages timers, and determines whether to invoke the callback
when the cooldown period expires.
The Trailing Edge Configuration
The behavior of the trailing edge is controlled by the
trailing option in
_.throttle(func, [wait=0], [options={}]). By default,
options.trailing is set to true.
// Default behavior: both leading and trailing calls are enabled
const throttled = _.throttle(updateData, 1000, { leading: true, trailing: true });When trailing is true, Lodash ensures that
if the throttled function is invoked at any point during an active
wait cooldown, the callback will execute one last time
after the timer completes.
Capturing Context and Arguments
Whenever the throttled wrapper is called during an active cooldown
period, Lodash does not discard the invocation entirely. Instead, it
captures the execution context (this) and the latest
arguments passed to the function:
- State Updates: Lodash updates internal references
(commonly
lastArgsandlastThis) with the newest parameters. - Overwriting Intermediate Calls: If multiple calls occur while waiting, Lodash continuously overwrites these variables with the most recent invocation's data. Only the latest state is retained for the final run.
The Cooldown Timer and Execution Decision
To schedule the trailing edge, Lodash calculates the remaining time
in the throttle window and sets an internal timer (via
setTimeout). When this timer completes, Lodash evaluates
whether a trailing execution is warranted:
- Check for Trailing Invocations: The timer checks
whether any calls were made during the cooldown window (verifying if
lastArgsexists). - Execute the Callback: If calls were recorded and
trailingis enabled, the underlying function executes immediately using the storedlastArgsandlastThis. - Reset State: After running,
lastArgsandlastThisare cleared toundefined. - No-Op Scenario: If no calls occurred during the cooldown window, the timer simply expires without invoking the function.
Trailing Edge Disabled
(trailing: false)
If you configure { trailing: false }, Lodash changes how
it handles invocations during the cooldown:
- Intermediate invocations do not schedule a trailing call.
- When the cooldown expires, no delayed function run occurs.
- The function will only run again if explicitly triggered by a new
call after the
waitperiod has completely elapsed.
Cancellation and Cleanup
Lodash provides a .cancel() method on the throttled
instance. Invoking .cancel() clears the pending
setTimeout timer and wipes the saved lastArgs
and lastThis. This guarantees that any scheduled trailing
execution is instantly aborted, preventing unexpected executions after a
component unmounts or a process ends.