Web DevPerformance & OptimizationInteractiveBeginner
Throttle
KO: 쓰로틀·EN: Throttle·JA: スロットル
A rate-limiting technique that ensures a high-frequency function executes at most once within every fixed time interval
Table of Contents
Interactive Playground
Tap or drag to interact with the demoQuick Answer
What is Throttle?
A rate-limiting technique that ensures a high-frequency function executes at most once within every fixed time interval. It is a core concept in Performance & Optimization within Web Dev.
Core Principles
Throttling caps how often a function can run during continuous high-frequency events—such as scroll, mousemove, or resize—by allowing at most one execution per fixed time window (for example, once every 100ms).
Unlike Debouncing, which waits until the user stops triggering events before firing once at the end, Throttling guarantees steady, periodic updates while an interaction is actively happening.
Practical Takeaways
- Choose Throttle vs. Debounce intentionally: Use Throttle when intermediate feedback matters (infinite scroll checks, scroll progress bars, live drag coordinates); use Debounce when only the final settled state matters (search-as-you-type API calls, resize recalculations).
- Mind
leadingandtrailingedges: Executing immediately on the first event (leading: true) keeps the UI snappy, while running one final time after the cooldown (trailing: true) ensures the final scroll position or cursor coordinate isn't silently dropped. - Use
requestAnimationFramefor visual updates: When throttling DOM style changes or canvas animations, syncing to the browser's paint cycle viarequestAnimationFrameavoids jitter better than an arbitrary millisecond timer.
Notable Examples
- Infinite scroll observers: Sampling scroll depth every 150–200ms rather than on every pixel scrolled to avoid main-thread jank.
- Collaborative cursor broadcasting: Tools like Figma and Miro throttle pointer coordinates over WebSockets every 30–50ms to balance smoothness with bandwidth.
- Rate-limited action buttons: Enforcing a brief cooldown on submit or game ability triggers to prevent accidental double-firing.
Practical Code Snippet
Copy and drop straight into your projectthrottle.tsTypeScript· Scroll Event Throttle Implementation
export function throttle<T extends (...args: unknown[]) => void>( fn: T, intervalMs = 150) { let lastRun = 0; return (...args: Parameters<T>) => { const now = Date.now(); if (now - lastRun >= intervalMs) { lastRun = now; fn(...args); } };} // Usage: runs at most once every 150ms while scrollingwindow.addEventListener("scroll", throttle(() => { updateScrollProgress(window.scrollY);}, 150));