Course contentsShow
← JavaScript
Lesson 2390 of 2,72349. Common Patterns and IdiomsFree lesson

Debounce vs Throttle: When to Use Which

Compare use cases: debounce for search input, throttle for scroll or resize handlers.

Debounce vs Throttle: When to Use Which

You'll learn when to choose debounce over throttle and vice versa based on real-world scenarios.

Now that you understand both techniques, the key question is: when should you use each one?

The Core Difference in Behavior

Debounce waits for a "quiet period" — it delays execution until the events stop coming. Throttle enforces a regular rhythm — it guarantees execution at consistent intervals while events are happening.

When to Use Debounce

Use debounce when you only care about the final state after user activity settles:

  • Search input: Wait until the user stops typing before sending an API request
  • Form validation: Validate after the user finishes editing a field
  • Window resize complete: Recalculate layout only when resizing ends
  • Auto-save: Save draft only after typing pauses
// Search example - wait for user to stop typing
const searchInput = document.querySelector('#search');
searchInput.addEventListener('input', debounce((e) => {
  fetchSearchResults(e.target.value); // Only fires after 300ms pause
}, 300));

When to Use Throttle

Use throttle when you need continuous feedback during an ongoing action:

  • Scroll handlers: Update scroll position indicator regularly while scrolling
  • Mouse move tracking: Track cursor position at controlled intervals
  • Window resize during: Update live preview while actively resizing
  • Game loop inputs: Process player controls at fixed rates
// Scroll example - update progress bar smoothly while scrolling
window.addEventListener('scroll', throttle(() => {
  updateScrollProgressBar(); // Fires every 100ms max during scroll
}, 100));

Key Takeaway: Use debounce when you want to wait for user activity to finish (search input, form validation), and throttle when you need regular updates during continuous activity (scroll, resize, mouse tracking).