Debouncing & Throttling
MediumIn plain terms
Debounce: run after pause in events (e.g. search input). Throttle: run at most every N ms (e.g. scroll). Both limit how often a function runs.
What you need to know
- •Debounce: after pause
- •Throttle: max frequency
- •Performance for events
Try it yourself
Copy the code below and run it in your browser console or a code editor:
const debounce = (fn, ms) => { let t; return (...a) => { clearTimeout(t); t = setTimeout(() => fn(...a), ms); }; };Learn more
Dive deeper with these trusted resources: