patternModerate
Debounce vs throttle — choosing the right rate limiter
Viewed 0 times
debouncethrottlerate limitscroll handlerrequestAnimationFramelodash
browser
Problem
Need to limit how often a function runs in response to rapid events (scroll, resize, input). Wrong choice affects UX and performance.
Solution
Debounce: waits until events STOP, then fires once. Use for: search-as-you-type, resize handlers, form validation. Throttle: fires at most once per interval. Use for: scroll, mousemove, rate-limited API calls. Libraries: lodash.debounce, lodash.throttle. For React: use useDeferredValue for render debouncing.
Why
Without rate limiting, scroll events fire hundreds of times per second. Debounce waits for a pause. Throttle samples at intervals. Wrong choice = laggy UX or wasted computation.
Revisions (0)
No revisions yet.