DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Debounce vs Throttle: When and How to Use Each
XLinkedInReddit
MediumFrontend Engineering

Debounce vs Throttle: When and How to Use Each

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • The Difference
  • Debounce Implementation
  • Throttle Implementation
  • When to Use Which
  • React Hooks Version
  • Leading vs Trailing Edge

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

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

// Usage: Search input
const searchInput = document.getElementById("search");
searchInput.addEventListener("input", debounce((e) => {
  fetchResults(e.target.value);
}, 300));

Throttle Implementation

function throttle(fn, limit) {
  let inThrottle = false;
  return function (...args) {
    if (!inThrottle) {
      fn.apply(this, args);
      inThrottle = true;
      setTimeout(() => inThrottle = false, limit);
    }
  };
}

// Usage: Scroll handler
window.addEventListener("scroll", throttle(() => {
  updateScrollProgress();
}, 100));

When to Use Which

Use CaseTechniqueWhy
Search inputDebounceWait for user to finish typing
Window resizeDebounceOnly care about final size
Scroll positionThrottleNeed periodic updates
Mouse moveThrottleNeed smooth tracking
Form submitDebounce (leading)Prevent double submits
API pollingThrottleRate limiting

React Hooks Version

function useDebounce(value, delay) {
  const [debounced, setDebounced] = useState(value);
  useEffect(() => {
    const timer = setTimeout(() => setDebounced(value), delay);
    return () => clearTimeout(timer);
  }, [value, delay]);
  return debounced;
}

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.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • The Difference
  • Debounce Implementation
  • Throttle Implementation
  • When to Use Which
  • React Hooks Version
  • Leading vs Trailing Edge

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.