How do you implement debouncing and throttling in JavaScript?
Learn how to control function execution rates using debouncing and throttling for performance optimization.
advancedAdvanced conceptsjavascriptperformancedebouncethrottle
Published: Nov 3, 2025
Advertisement
Question
How do you implement debouncing and throttling in JavaScript?
Answer
Both debounce and throttle control how frequently a function runs.
- Debounce: waits for a pause before executing.
- Throttle: ensures a function runs at most once in a given interval.
// Debounce
function debounce(fn, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
// Throttle
function throttle(fn, limit) {
let inThrottle;
return (...args) => {
if (!inThrottle) {
fn(...args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}
Real-World Example
Debounce: search bar queriesThrottle: infinite scrolling or window resizing
Quick Practice
Implement a debounced function that logs input value only after the user stops typing for 500ms.
Summary
Debounce = “wait until quiet.”
Throttle = “limit the rate.”
Both are essential for frontend performance optimization.
Related Videos
Watch these videos to learn more about this topic
Frequently Asked Questions
When should I use debounce vs throttle?
Use debounce for actions triggered after typing stops (search). Use throttle for continuous actions (scroll, resize).
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement