🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Debounce & Throttle

AI & DATA SCIENCE // debounce-throttle

Debounce delays a function until after a wait period since last call. Throttle ensures a function runs at most once per interval.

Syntax

const debounced = debounce(fn, 300);
const throttled = throttle(fn, 100);

Deep Dive Course

**Debounce** waits for a quiet period — useful for search-as-you-type (fire API call only after user stops typing). **Throttle** limits execution rate — useful for scroll/resize handlers (run at most once per 100ms). Both are essential for performance optimization when dealing with high-frequency events.

1Understanding Debounce & Throttle

Debounce waits for a quiet period — useful for search-as-you-type (fire API call only after user stops typing). Throttle limits execution rate — useful for scroll/resize handlers (run at most once per 100ms). Both are essential for performance optimization when dealing with high-frequency events.

💡

Use debounce for 'wait until done' (search input). Use throttle for 'limit rate' (scroll, resize, mouse move).

editor.html
// Debounce implementation
function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

// Search input fires only 300ms after typing stops
const searchInput = document.querySelector('#search');
searchInput.addEventListener('input', debounce((e) => {
  fetchResults(e.target.value);
}, 300));
localhost:3000

2Practical Example

Here is a real-world application of Debounce & Throttle showing how it is used in production JavaScript code.

editor.html
// Throttle implementation
function throttle(fn, interval) {
  let last = 0;
  return function(...args) {
    const now = Date.now();
    if (now - last >= interval) {
      last = now;
      fn.apply(this, args);
    }
  };
}

// Scroll handler fires at most 10 times per second
window.addEventListener('scroll', throttle(() => {
  updateScrollIndicator();
}, 100));
localhost:3000

3Best Practices

Follow these guidelines when working with Debounce & Throttle:

1. Use debounce for search inputs, form validation, resize handlers

2. Use throttle for scroll events, infinite scroll, rate-limited APIs

3. Always clean up debounced/throttled timers on component unmount

⚠️

Tip: Use debounce for 'wait until done' (search input). Use throttle for 'limit rate' (scroll, resize, mouse move).

editor.html
// Debounce implementation
function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

// Search input fires only 300ms after typing stops
const searchInput = document.querySelector('#search');
searchInput.addEventListener('input', debounce((e) => {
  fetchResults(e.target.value);
}, 300));
localhost:3000

Examples

Example 01Basic Usage
// Debounce implementation
function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

// Search input fires only 300ms after typing stops
const searchInput = document.querySelector('#search');
searchInput.addEventListener('input', debounce((e) => {
  fetchResults(e.target.value);
}, 300));
Example 02Advanced Example
// Throttle implementation
function throttle(fn, interval) {
  let last = 0;
  return function(...args) {
    const now = Date.now();
    if (now - last >= interval) {
      last = now;
      fn.apply(this, args);
    }
  };
}

// Scroll handler fires at most 10 times per second
window.addEventListener('scroll', throttle(() => {
  updateScrollIndicator();
}, 100));

Best Practices

  • Use debounce for search inputs, form validation, resize handlers
  • Use throttle for scroll events, infinite scroll, rate-limited APIs
  • Always clean up debounced/throttled timers on component unmount

Interview Question

What is the difference between debounce and throttle?

Hint: Delay after quiet vs rate limit.

Debounce: delays the function until N ms AFTER the last call. During rapid calls, only the final one executes (after silence). Throttle: allows the function at most once per N ms. During rapid calls, it fires at regular intervals, ignoring the rest. Use debounce when you want the 'final result' (search), throttle when you want 'regular updates' (scroll position).

Exercises

MediumPractice using Debounce & Throttle in a real scenario.
View Solution
// Debounce implementation
function debounce(fn, delay) {
  let timer;
  return function(...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

// Search input fires only 300ms after typing stops
const searchInput = document.querySelector('#search');
searchInput.addEventListener('input', debounce((e) => {
  fetchResults(e.target.value);
}, 300));

Frequently Asked Questions

What is the difference between debounce and throttle?

Debounce: delays the function until N ms AFTER the last call. During rapid calls, only the final one executes (after silence). Throttle: allows the function at most once per N ms. During rapid calls, it fires at regular intervals, ignoring the rest. Use debounce when you want the 'final result' (search), throttle when you want 'regular updates' (scroll position).

Related Functions

Callback-FunctionssetTimeoutEvent-ListenersClosures