DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • Scope & Closures
  • Asynchronous JavaScript
  • DOM Manipulation
  • ES6+ to 2026
  • Execution Context & Call Stack
  • Hoisting: var, let, const & Functions
  • Data Types & Type Coercion
  • Prototypes & the this Keyword
  • The Event Loop Deep Dive
  • Higher-Order Functions, Currying & Functional Patterns
  • Callbacks, Promises & Async/Await
  • Event Delegation & DOM Optimization
  • Destructuring, Spread/Rest & Modern Syntax
  • ES Modules & the Fetch API
  • Memory Management & Garbage Collection
  • Debouncing, Throttling & Performance Patterns
  • Design Patterns in JavaScript
  • Proxies, Reflect & Symbols
  • JavaScript Optimizations & Best Practices
  • JavaScript Interview Questions (30 MCQs)
← From Beginner to Expert in Fundamentals of Frontend

Debouncing, Throttling & Performance Patterns

18 min read
DebounceThrottlerequestAnimationFrameWeb WorkersrequestIdleCallbackPerformanceRate Limiting

Controlling Function Execution Rate

Debounce

Wait until the user stops performing an action for a specified delay before executing.

Use case: Search input, window resize, form validation.

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

// Usage: Only search after user stops typing for 300ms
const search = debounce((query) => {
  fetch(`/api/search?q=${query}`);
}, 300);

input.addEventListener("input", (e) => search(e.target.value));

Throttle

Execute at most once per specified interval, regardless of how many times it's triggered.

Use case: Scroll events, mouse move, API rate limiting.

function throttle(fn, interval) {
  let lastTime = 0;
  return function(...args) {
    const now = Date.now();
    if (now - lastTime >= interval) {
      lastTime = now;
      fn.apply(this, args);
    }
  };
}

// Usage: Update scroll position at most every 100ms
window.addEventListener("scroll", throttle(() => {
  updateScrollProgress();
}, 100));

Debounce vs Throttle

FeatureDebounceThrottle
ExecutionAfter action stopsDuring action, periodically
Best forSearch, validationScroll, resize, drag
User types fastFires once at the endFires every N ms

requestAnimationFrame

// Smooth animation at 60fps
function animate(element) {
  let position = 0;
  
  function step() {
    position += 2;
    element.style.transform = `translateX(${position}px)`;
    
    if (position < 300) {
      requestAnimationFrame(step);
    }
  }
  
  requestAnimationFrame(step);
}

Web Workers for Heavy Computation

// main.js
const worker = new Worker("worker.js");
worker.postMessage({ data: largeArray });
worker.onmessage = (e) => {
  console.log("Result:", e.data);
};

// worker.js
self.onmessage = (e) => {
  const result = heavyComputation(e.data);
  self.postMessage(result);
};

requestIdleCallback

// Run non-critical tasks when browser is idle
requestIdleCallback((deadline) => {
  while (deadline.timeRemaining() > 0) {
    doNonCriticalWork();
  }
});

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.