Implement throttle
Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.
Implement throttle(func, wait). It returns a new function that calls func right away, then ignores every call for the next wait milliseconds. Once the wait is over, the next call goes through again.
Throttling keeps a handler running at a steady rate during a long burst, such as scroll or mousemove events, instead of waiting for the burst to end like debounce does.
let i = 0
const increment = () => i++
const throttled = throttle(increment, 100)
// t = 0
throttled()
// i is now 1
// t = 50: still inside the 100ms window, ignored
throttled()
// i is still 1
// t = 101: the window is over
throttled()
// i is now 2Requirements
- The first call runs
funcimmediately, with that call's arguments. - Calls during the next
waitms are ignored. They do not extend the window. - After
waitms, the next call runsfuncimmediately and starts a new window. funcruns with the samethisthe throttled function was called with.- Each throttled function has its own window.
Note
You don't need a trailing call (running the last ignored call when the window ends), or the leading, trailing and cancel options Lodash's throttle has.
Approach
The throttled function only needs one piece of state: whether it is inside a window right now. A closure keeps a private waiting flag per throttled function.
On every call:
- If
waitingistrue, return without doing anything. - Otherwise call
funcwith the caller'sthisand arguments, setwaiting = true, and start a timer that sets it back tofalseafterwaitms.
Ignored calls never touch the timer, which is the key difference from debounce: there, every call restarts the wait.
Keeping this
As with debounce, the returned function must be a regular function so it receives the caller's this, and the call to func uses func.apply(this, args).
Complexity
Each call is O(1) time and each throttled function uses O(1) memory: one boolean and at most one pending timer.
Follow-ups interviewers ask
- Trailing call: remember the arguments of the last ignored call and run it when the window ends, so the final state of a burst is never lost.
- Timestamps instead of timers: store the time of the last run and compare with
Date.now(). It avoids a timer per window, but makes a trailing call harder. - Throttle vs. debounce: throttle runs at most once per window during a burst; debounce runs once after the burst ends.
Reference solution
/**
* @param {Function} func
* @param {number} wait
* @returns {Function}
*/
export default function throttle(func, wait) {
let waiting = false
return function throttled(...args) {
if (waiting) return
waiting = true
func.apply(this, args)
setTimeout(() => {
waiting = false
}, wait)
}
}export default function throttle<Args extends unknown[]>(
func: (this: unknown, ...args: Args) => void,
wait: number,
): (this: unknown, ...args: Args) => void {
let waiting = false
return function throttled(this: unknown, ...args: Args) {
if (waiting) return
waiting = true
func.apply(this, args)
setTimeout(() => {
waiting = false
}, wait)
}
}