Lodash Throttle: Leading vs Trailing Explained
The _.throttle method in the Lodash JavaScript library
limits how frequently a function can execute over time. By default, a
throttled function can invoke at the start of the wait period, the end
of the wait period, or both. This behavior is controlled by two boolean
options: leading and trailing. Understanding
the difference between these two settings allows developers to optimize
event listeners—such as window resizing, scrolling, or button clicks—to
execute exactly when desired.
Understanding the Throttle Mechanism
When you wrap a function with
_.throttle(fn, wait, options), Lodash creates a cooldown
window defined by the wait duration in milliseconds.
_.throttle(func, [wait=0], [options={}])The options object accepts:
leading: A boolean indicating whether to invoke the function on the leading edge of the timeout (immediately when the event starts). The default istrue.trailing: A boolean indicating whether to invoke the function on the trailing edge of the timeout (after the wait interval has passed). The default istrue.
The leading Option
The leading option dictates whether the throttled
function executes on the very first trigger before the cooldown interval
begins.
leading: true(Default): The function runs immediately on the first event call. The cooldown timer begins immediately afterward.leading: false: The function suppresses execution on the first trigger. It will not run immediately and must wait for the timer interval to elapse.
The trailing Option
The trailing option dictates whether the function
executes one final time after the cooldown period expires, provided the
function was called again during that interval.
trailing: true(Default): If the event fires one or more times during the cooldown period, the function will execute one last time at the end of the interval using the most recent arguments.trailing: false: Calls that occur during the cooldown period are ignored. Once the initial execution runs, no execution will happen at the end of the window.
Behavior Combinations
1.
{ leading: true, trailing: true } (Default)
The function runs immediately upon the first trigger. If additional triggers happen during the wait duration, the function runs once more at the end of the interval with the latest arguments.
- Best used for: Continuous operations where both an instant response and the most updated final state are required, such as real-time search input autocomplete or window resize recalculations.
2.
{ leading: true, trailing: false }
The function runs immediately upon the first trigger, but any subsequent triggers during the wait period are completely discarded. The function will not fire again until the wait duration expires and a new event occurs.
- Best used for: Preventing double submissions, such as user clicks on a submit button or rapid button-mashing actions.
3.
{ leading: false, trailing: true }
The function does not run immediately when the event starts. Instead, it waits for the duration to elapse and then runs using the most recent event data.
- Best used for: Scroll-based calculations where only the resting state matters rather than immediate feedback during the start of the movement.
4.
{ leading: false, trailing: false }
Setting both options to false disables execution
entirely. Lodash requires at least one option to be true
for the throttled function to invoke; setting both to false
creates a no-op function that will never execute.