Skip to content

Latest commit

 

History

History
104 lines (71 loc) · 3.26 KB

File metadata and controls

104 lines (71 loc) · 3.26 KB

📘 Episode 4: Debouncing in JavaScript

🧠 Overview

Debouncing is a programming pattern used to limit the rate at which a function gets invoked. This is especially useful in scenarios like search input fields, resizing windows, or scrolling, where frequent function calls can degrade performance.


🔹 What is Debouncing?

  • Debouncing ensures that a function is not called again until a certain amount of time has passed since the last call.
  • It is a performance optimization technique.
  • Ideal for input validation, autosuggestions, or API calls during typing.

🛠️ Debounce Function Implementation

⚡ The Problem

// Without debouncing - fires on every keystroke
function getData() {
  const inputField = document.getElementById("inputField");
  console.log(inputField.value);
}

document.getElementById("inputField").addEventListener("input", getData);

// Typing "hello" = 5 function calls (h, he, hel, hell, hello)

🛠️ The Solution

// Production-ready debounce function
function debounce(func, delay = 300) {
  let timeoutId;
  
  return function debounced(...args) {
    // Clear previous timer
    clearTimeout(timeoutId);
    
    // Set new timer
    timeoutId = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}

// Usage: getData will only run 300ms after the last call
const debouncedGetData = debounce(getData, 300);

�� How It Works

  1. Clear previous timer when function is called again
  2. Set new timer for the delay period
  3. Execute function only after delay of inactivity
  4. Result: Typing "hello" = 1 function call (after 300ms)

🧠 Real-World Use Cases

  • Search bars (avoid sending API calls on every keypress)
  • Resize events (adjust layout without excessive DOM computation)
  • Scroll listeners (like hiding/showing headers or loading data on scroll end)

🤔 Interview Questions

  1. What is debouncing?

    • A technique to delay function execution until after a defined period of inactivity.
  2. Why is debouncing useful?

    • To reduce unnecessary function calls and improve performance, especially in UI-heavy applications.
  3. How is debouncing different from throttling?

    • Debounce: waits until user stops triggering the event.
    • Throttle: invokes at regular intervals during continuous activity.
  4. How would you implement a debounce function from scratch?

    • Use a timer (setTimeout) to delay function execution and clear it if called again within the delay.
  5. Does debounce retain the context (this)?

    • Yes, using func.apply(this, args) helps retain original function context.

✅ Key Takeaways

  • Debouncing is useful when a user triggers an event repeatedly in quick succession.
  • Helps avoid performance issues in apps with frequent state updates or API calls.
  • It's crucial for good UX, especially in search fields, input validations, and live filtering.
  • Always return a function and use setTimeout and clearTimeout for correct debounce behavior.
  • func.apply(this, args) ensures the function runs in the correct context.

🎥 Watch This Topic on YouTube