Lodash _.throttle() method
What you’ll learn
- How throttle caps work during never-ending event bursts.
- How
leadingandtrailingschedule edge invocations. - When to call
cancel()/flush()during teardown. - How throttle complements
_.debouncefor UX tuning.
Prerequisites
Read _.debounce(), _.spread(), and the Function hub.
- Timers: asynchronous scheduling via
setTimeout. - DOM events: scroll, resize, pointermove firing faster than paint.
Overview
_.throttle(func, wait, options) returns a wrapper that enforces a minimum spacing between executions—perfect for sensors that never pause yet still need periodic sampling.
Syntax
_.throttle(func, [wait=0], [options])- func: callback to throttle.
- wait: milliseconds between allowed invocations.
- options:
leadingandtrailingbooleans (defaultstrue). - returns: throttled function with
cancelandflushhelpers.
Default leading + trailing burst
A tight synchronous loop triggers the leading call immediately and typically schedules one trailing flush once the wait window elapses.
import throttle from "lodash/throttle";
let runs = 0;
const tick = throttle(() => {
runs += 1;
}, 100);
for (let i = 0; i < 30; i += 1) {
tick();
}
setTimeout(() => console.log(runs), 150);
// Often prints 2 with lodash defaultsTrailing-only sampling
Disable the leading edge when you only want updates after each quiet slice—common for gentler scroll projections.
import throttle from "lodash/throttle";
let runs = 0;
const smooth = throttle(() => {
runs += 1;
}, 80, { leading: false, trailing: true });
for (let i = 0; i < 10; i += 1) {
smooth();
}
setTimeout(() => console.log(runs), 120);
// 1 — trailing fires once after the burst settlescancel() drops pending trailing work
Leading invocation still counts, but cancel clears timers before trailing callbacks execute.
import throttle from "lodash/throttle";
let runs = 0;
const risky = throttle(() => {
runs += 1;
}, 100);
risky();
risky();
risky.cancel();
setTimeout(() => console.log(runs), 200);
// 1 — only the leading run survives📋 _.throttle vs _.debounce
| Helper | Best for | Behavior |
|---|---|---|
_.throttle | Scroll, resize, drag streams | Fire on a schedule while events continue |
_.debounce | Search typing, autosave | Wait until calls pause |
Pitfalls to avoid
Listeners after teardown
Call cancel() when removing listeners so trailing timers cannot touch disposed DOM.
Unexpected double fire
Leading plus trailing can surprise newcomers—toggle edges explicitly when you need strictly-one semantics.
Visual smoothness
Pair requestAnimationFrame with throttled data fetching when frames matter more than timer granularity.
❓ FAQ
Summary
- Purpose: throttle guarantees bounded invocation rates during noisy streams.
- Controls: leading/trailing toggles plus
cancel()/flush(). - Next: Lodash _.unary(), revisit Lodash _.spread(), contrast _.debounce(), or read official _.throttle docs.
With lodash defaults (leading: true, trailing: true), the first call in a burst runs immediately and a trailing invocation may fire after the wait window if additional calls occurred—explaining why synchronous spam often yields two executions, not one.
6 people found this page helpful
