Lodash _.throttle() method

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How throttle caps work during never-ending event bursts.
  • How leading and trailing schedule edge invocations.
  • When to call cancel() / flush() during teardown.
  • How throttle complements _.debounce for 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

javascript
_.throttle(func, [wait=0], [options])
  • func: callback to throttle.
  • wait: milliseconds between allowed invocations.
  • options: leading and trailing booleans (defaults true).
  • returns: throttled function with cancel and flush helpers.
1

Default leading + trailing burst

A tight synchronous loop triggers the leading call immediately and typically schedules one trailing flush once the wait window elapses.

javascript
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 defaults
2

Trailing-only sampling

Disable the leading edge when you only want updates after each quiet slice—common for gentler scroll projections.

javascript
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 settles
3

cancel() drops pending trailing work

Leading invocation still counts, but cancel clears timers before trailing callbacks execute.

javascript
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

HelperBest forBehavior
_.throttleScroll, resize, drag streamsFire on a schedule while events continue
_.debounceSearch typing, autosaveWait until calls pause

Pitfalls to avoid

Unmount

Listeners after teardown

Call cancel() when removing listeners so trailing timers cannot touch disposed DOM.

Defaults

Unexpected double fire

Leading plus trailing can surprise newcomers—toggle edges explicitly when you need strictly-one semantics.

Paint

Visual smoothness

Pair requestAnimationFrame with throttled data fetching when frames matter more than timer granularity.

❓ FAQ

It returns a wrapper that invokes func at most once per wait milliseconds while calls keep arriving—optionally on the leading edge, trailing edge, or both.
Debounce waits for quiet time after the last call. Throttle guarantees periodic execution during continuous bursts—better for scroll and resize listeners.
Yes—lodash mirrors debounce ergonomics: cancel() drops pending work; flush() forces a pending trailing invocation when configured.
Use rAF when updates must align with paint cycles; throttle stays useful for non-visual streams or when wait is wall-clock based.
Use import throttle from "lodash/throttle" for tree-shaking friendly bundles.

Summary

Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful