Debounce & Throttle Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Debounce & Throttle runs in O(1) time and O(1) space, in JavaScript as in every other language.
- ✓setTimeout and clearTimeout are the whole toolkit, and they are enough. Both debounce and throttle are a closure over a timer id.
- ✓Return a function that also exposes .cancel(), because a debounced callback that survives component teardown will fire against a destroyed target.
- ✓The JavaScript trap: In React the debounced function must be created ONCE. Defining it inside the component body recreates it on every render, so each keystroke gets a brand-new timer, nothing is ever cancelled, and the "debounced" handler fires on every single keystroke, the exact behaviour it was added to prevent.
- ✓setTimeout has a clamped minimum of 4ms once nested more than five deep, so a "1ms throttle" does not exist in the browser.
What Debounce & Throttle actually is
Debounce & Throttle in JavaScript: what you actually reach for
JavaScript implementation
function debounce(fn, wait) {
let timer = null;
function debounced(...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), wait);
}
debounced.cancel = () => clearTimeout(timer);
return debounced;
}
function throttle(fn, interval) {
let last = 0;
let trailing = null;
return function throttled(...args) {
const now = performance.now(); // monotonic, unlike Date.now()
const remaining = interval - (now - last);
if (remaining <= 0) {
last = now;
fn.apply(this, args);
} else if (trailing === null) {
// keep the LAST call so the final state is never dropped
trailing = setTimeout(() => {
last = performance.now();
trailing = null;
fn.apply(this, args);
}, remaining);
}
};
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Debounce & Throttle?
setTimeout and clearTimeout are the whole toolkit, and they are enough. Both debounce and throttle are a closure over a timer id. For the throttle clock use performance.now() in the browser rather than Date.now(): performance.now() is monotonic, so it cannot jump when the system clock is adjusted.
What is the most common JavaScript mistake when implementing Debounce & Throttle?
In React the debounced function must be created ONCE. Defining it inside the component body recreates it on every render, so each keystroke gets a brand-new timer, nothing is ever cancelled, and the "debounced" handler fires on every single keystroke, the exact behaviour it was added to prevent. Wrap it in useMemo or useRef, and cancel it in the effect cleanup.
How does Debounce & Throttle actually perform in JavaScript?
setTimeout has a clamped minimum of 4ms once nested more than five deep, so a "1ms throttle" does not exist in the browser. Background tabs clamp far harder, to once per second or worse, which means a throttled poller silently slows to a crawl when the tab loses focus. For per-frame work use requestAnimationFrame instead of a throttle, it is the correct rate limiter for rendering. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Replit, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.