JavaScript·Intermediate·Coding·1 min read
How do debounce and throttle differ?
Short interview answer
Debounce waits for a quiet period before invoking, so a burst collapses into one call. Throttle limits invocation to at most once per interval while activity continues. Search suggestions often debounce; continuous scroll measurement may throttle or align work to animation frames.
Example
function debounce(fn, delay) { let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), delay); // fires only after calls stop };}
function throttle(fn, interval) { let last = 0; return (...args) => { const now = Date.now(); if (now - last >= interval) { last = now; fn(...args); } // at most 1/interval };}Key takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.