These two patterns prevent performance disasters in event-heavy UIs. I've seen production apps grind to a halt without them.
The Difference
Debounce: Wait until the user STOPS doing something, then execute once.
Throttle: Execute at most once every N milliseconds, regardless of how many events fire.
Debounce Implementation
Throttle Implementation
When to Use Which
| Use Case | Technique | Why |
|---|---|---|
| Search input | Debounce | Wait for user to finish typing |
| Window resize | Debounce | Only care about final size |
| Scroll position | Throttle | Need periodic updates |
| Mouse move | Throttle | Need smooth tracking |
| Form submit | Debounce (leading) | Prevent double submits |
| API polling | Throttle | Rate limiting |
React Hooks Version
Leading vs Trailing Edge
Default debounce fires on the trailing edge (after the delay). Leading edge fires immediately on the first call, then ignores subsequent calls during the delay. Use leading edge for button clicks to prevent double-submit.