Skip to main content
02

Implement throttle

Mediumclosurestimersthis

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.

js
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 2

Requirements

  • The first call runs func immediately, with that call's arguments.
  • Calls during the next wait ms are ignored. They do not extend the window.
  • After wait ms, the next call runs func immediately and starts a new window.
  • func runs with the same this the 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:

  1. If waiting is true, return without doing anything.
  2. Otherwise call func with the caller's this and arguments, set waiting = true, and start a timer that sets it back to false after wait ms.

Ignored calls never touch the timer, which is the key difference from debounce: there, every call restarts the wait.

Loading diagram...

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

js
/**
 * @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)
  }
}
ts
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)
  }
}