What is debouncing and throttling?
Answer
Both techniques limit how often a function executes during rapid repeated invocations — useful for scroll, resize, input, and mousemove events. Debouncing delays execution until after a specified quiet period — the function only fires after the user has stopped triggering the event for N milliseconds. If triggered again before the delay expires, the timer resets. Use for: search input (wait until the user stops typing before making an API call), window resize (only recalculate after resize ends). Implementation: function debounce(fn, delay) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), delay); }; }. Throttling ensures the function executes at most once per N milliseconds, regardless of how many times it is triggered. Use for: scroll event handlers, rate-limiting button clicks, gaming input. Implementation uses a timestamp or flag to skip calls within the period. Lodash provides battle-tested _.debounce() and _.throttle().