Facebook Pixel

Debounce vs Throttle: Implementation Comparison

Side-by-side implementation of debounce and throttle.

Debounce vs Throttle: Implementation Comparison

Debounce

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

Key: clearTimeout + setTimeout. Each call resets the timer.

Throttle

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

Key: Date.now() + comparison. Execute only if enough time has passed.

Key Differences in Code

FeatureDebounceThrottle
UsessetTimeout + clearTimeoutDate.now() comparison
Statetimer (number)last (timestamp)
RunsAfter delay, only last callImmediately if interval passed
CancelsPrevious timersNothing (just skips)

Advanced: Throttle with Trailing Call

function throttleWithTrailing(fn, delay) { let last = 0; let timer; return function (...args) { const now = Date.now(); const remaining = delay - (now - last); if (remaining <= 0) { clearTimeout(timer); last = now; fn.apply(this, args); } else { clearTimeout(timer); timer = setTimeout(() => { last = Date.now(); fn.apply(this, args); }, remaining); } }; }

This ensures the last call also executes (trailing call), which the basic throttle does not do.

The Takeaway

Debounce: setTimeout + clearTimeout (reset on each call). Throttle: Date.now() + comparison (execute if interval passed). Advanced throttle with trailing: combine both (execute immediately if interval passed, otherwise schedule a trailing call).

Debounce uses setTimeout + clearTimeout (reset on each call, only last runs). Throttle uses Date.now() comparison (execute only if enough time has passed since last execution).

Debounce needs a timer variable (number from setTimeout). Throttle needs a last timestamp (number from Date.now()). Both use closures to keep the state alive across calls.

When the last call within an interval is also executed (after the interval ends). The basic throttle drops calls within the interval. The trailing version schedules the last call to run after the interval, ensuring no call is lost.

Yes. If a call happens within the interval (now - last < delay), it is simply ignored. No timer is set. The call is lost. Use the trailing version if you need the last call to execute.

Calculate remaining = delay - (now - last). If remaining <= 0, execute immediately. Otherwise, set a timer to execute after remaining ms. Clear the previous timer on each call. This ensures the last call executes.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.