Throttle in JavaScript is a rate-limiting technique that wraps a function so it runs at most once per fixed time interval, no matter how many times the triggering event fires. In Browser JavaScript, throttling keeps handlers for high-frequency events such as scroll, resize, pointermove and wheel from executing hundreds of times per second, which protects the main thread and keeps the interface responsive.
A throttled function still reacts during a continuous burst of activity, which is the key difference from Debounce, where execution waits until the activity stops. Throttling is therefore the usual choice when the user must see steady, periodic feedback while an action is still in progress.
How a throttled function controls execution
A throttle wrapper works by remembering when the wrapped function last ran and ignoring or postponing calls that arrive before the interval has elapsed. The wrapper is a closure that stores a timestamp or a timer identifier, and every incoming event checks that state before deciding whether to call the original function.
Two implementation strategies dominate. The timestamp approach compares Date.now() or performance.now() with the last execution time and calls the function immediately when enough time has passed. The timer approach schedules a single setTimeout and drops further calls until the timer fires. Production-grade helpers combine both, so the first call runs at once and the last call in a burst is not lost.
The table below shows the main configuration options that throttle helpers expose and what each one changes.
| Option | Typical values | What it controls |
|---|---|---|
| Wait interval | 16 to 250 ms | Minimum gap between two executions |
| Leading edge | true or false | Whether the first event runs immediately |
| Trailing edge | true or false | Whether a final call runs after the burst |
| Cancel method | Optional function | Discards a pending trailing call |
| Flush method | Optional function | Runs a pending trailing call at once |
With both edges enabled, a burst of 100 events over one second and a 200 ms interval produces roughly five or six executions: one at the start, regular calls during the burst and a final call carrying the latest arguments. The wrapper should also preserve this and the most recent arguments, or the handler may act on stale event data.
Throttle compared with debounce and requestAnimationFrame
Throttle, debounce and requestAnimationFrame all reduce how often work runs, but they differ in when the work happens. Choosing the wrong one produces either laggy feedback or wasted computation, so the distinction matters in everyday frontend engineering.
The following comparison summarizes the timing behavior of each technique during a continuous stream of events.
| Technique | When the function runs | Best suited for |
|---|---|---|
| Throttle | At regular intervals during the burst | Scroll position tracking, drag feedback |
| Debounce | Once, after events stop for a set delay | Search-as-you-type, form validation |
| requestAnimationFrame | Once per display frame, before paint | Visual updates and animations |
| Intersection Observer API | When visibility thresholds are crossed | Lazy loading, viewport detection |
A requestAnimationFrame-based throttle is a special case: it limits execution to the display refresh rate, usually 60 Hz but 120 Hz or more on modern screens, and aligns work with rendering. It is ideal for DOM reads and writes that affect layout, while a time-based throttle suits network requests or analytics that should not depend on frame rate.
For detecting whether elements enter the viewport, the Intersection Observer API often replaces a throttled scroll listener entirely, because the browser computes intersections off the main thread and notifies the page only when something changes.
Choosing an interval and configuration
The right interval depends on what the user perceives and how expensive each execution is. Intervals close to one frame feel continuous, while longer intervals save more work but make updates visibly step-like.
This decision table maps common situations to a sensible throttle configuration.
| Situation | Suggested setup | Reason |
|---|---|---|
| Sticky header on scroll | requestAnimationFrame throttle | Updates must match the rendered frame |
| Infinite scroll loading | 200 to 300 ms, trailing edge on | Network calls are costly; the final position matters |
| Window resize recalculation | 100 to 150 ms, both edges | Layout feels responsive without thrashing |
| Pointer tracking for analytics | 500 to 1000 ms | Coarse sampling is sufficient |
| Button that triggers an API call | 1000 ms, leading edge only | Prevents accidental double submission |
Developers should create the throttled function once and reuse the same reference. Calling a throttle helper inside a render function or an event handler produces a new wrapper on every call, which silently disables throttling because each wrapper has its own empty state. In component frameworks, the wrapper typically lives in a memoized value or an instance field.
Removing a throttled handler with removeEventListener also requires the exact same function reference that was registered. Code that passes a fresh throttle(handler, 100) call to the removal method leaves the original listener attached and causes a memory leak.
Throttling in the event loop and browser performance
Throttling matters because Browser JavaScript runs event handlers on a single main thread shared with style calculation, layout and painting. When a scroll handler takes 8 ms and fires on every scroll event, it can consume most of a 16.7 ms frame budget at 60 Hz and cause visible jank.
Timer-based throttles depend on the Event loop: a scheduled setTimeout callback runs only when the call stack is empty, so the real interval is a minimum, not a guarantee. Browsers also clamp timers in background tabs, often to one second or longer, which means a throttled function may run far less often than configured when the page is hidden.
Throttling complements other performance tools rather than replacing them. Passive event listeners let scrolling proceed without waiting for the handler, and a Web worker can move heavy computation off the main thread entirely. Throttle reduces how often work runs; it does not make each execution cheaper.
Common mistakes and how to avoid them
Most throttling bugs come from lost state, lost arguments or the wrong technique. The table lists frequent symptoms with their causes and fixes.
| Symptom | Likely cause | Fix |
|---|---|---|
| Handler runs on every event | Wrapper recreated on each call | Create the throttled function once |
| Final scroll position ignored | Trailing edge disabled | Enable the trailing call |
| Wrong element or context used | Lost this binding | Use apply with stored context |
| Errors after component removal | Pending trailing call fires late | Call cancel during cleanup |
| Choppy animation | Time interval longer than a frame | Switch to requestAnimationFrame |
Another misconception is that throttling reduces server load by itself. It reduces the number of client-side calls, but if each call sends a request, the server still receives a steady stream; caching, request cancellation with AbortController or server-side rate limits remain necessary for robust API protection.
Stripped to the essentials
Some browser events fire extremely often. Scrolling a page can trigger dozens of signals every second, and running heavy code for each one makes the page slow and jerky.
Throttling puts a speed limit on a function. It lets the code run once, then ignores new requests until a short pause has passed, then allows it again. Think of a turnstile that lets one person through every few seconds, however large the crowd. The page still responds while the user scrolls, just not more often than it needs to.
Where it is applied
A frontend developer at an online news publisher builds a reading progress bar and a lazy loader that fetches the next article as readers approach the bottom of a long page. The first version attached a plain scroll listener that recalculated positions and checked whether to load content on every event, firing more than 100 times per second on fast trackpads.
The developer moves the progress bar update into a requestAnimationFrame throttle and wraps the loading check in a 250 ms throttle with the trailing edge enabled, so the final scroll position is always evaluated. The throttled functions are created once at startup and canceled when the reader leaves the article view.
Profiling on a mid-range phone shows scripting time during scrolling dropping by roughly three quarters, dropped frames almost disappearing and the next article still loading on time. The result is smooth scrolling with no loss of functionality.