Debounce vs Throttle: Real-World Use Cases
Real-world examples of when to use debounce and throttle.
Debounce vs Throttle: Real-World Use Cases
Debounce Use Cases
// Search input (300ms) const search = debounce((q) => fetchResults(q), 300); input.addEventListener("input", (e) => search(e.target.value)); // Auto-save (1000ms) const save = debounce(() => saveDocument(), 1000); editor.addEventListener("input", save); // Window resize (250ms) const onResize = debounce(() => recalculateLayout(), 250); window.addEventListener("resize", onResize);
Throttle Use Cases
// Scroll (100ms) const onScroll = throttle(() => updateScrollPosition(), 100); window.addEventListener("scroll", onScroll); // Mousemove (50ms) const onMouseMove = throttle((e) => updateCursor(e), 50); document.addEventListener("mousemove", onMouseMove); // Button click (500ms, prevent rapid clicks) const onClick = throttle(() => submitForm(), 500); button.addEventListener("click", onClick);
The Takeaway
Debounce: search (300ms), auto-save (1000ms), resize (250ms). Throttle: scroll (100ms), mousemove (50ms), button click (500ms). The key: debounce waits for activity to stop; throttle rate-limits during activity.
Search input (300ms, wait for typing to stop), auto-save (1000ms, wait for editing to stop), window resize (250ms, wait for resizing to stop), and form validation (500ms, wait for input to stop).
Scroll (100ms, update position at a controlled rate), mousemove (50ms, track cursor at a controlled rate), and button click (500ms, prevent rapid double-clicks).
Debounce. Wait for the user to stop typing, then make the API call. This avoids calling the API on every keystroke. 300ms is a good delay.
Throttle. The user needs continuous feedback while scrolling. Debounce would delay the update until scrolling stops, which feels laggy. Throttle at 100ms gives smooth feedback.
Debounce. Wait for the user to stop editing, then save. This avoids saving on every keystroke. 1000ms is a good delay (save 1 second after the last edit).
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

