Table of contents

Debounce and Throttle in Javascript

Debounce and Throttle in Javascript

Debounce and throttle are design patterns used to limit the execution of functions, generally they are used to restrict the amount of times an event is fired: click, scroll, resize or other events. Patterns are not exclusive to Javascript; in a previous post I explained how to use throttle to limit the number of requests received by the nginx server.

Both patterns generate a function that receives a callback and a timeout or delay.

Debounce

The debounce pattern postpones the execution of a function until a certain waiting time has elapsed.

Further attempts to execute the function will cancel the pending execution and restart the timeout.

Simplified debounce pattern schematic

debounce explanation

The code for debounce in javascript looks like this:

const debounce = (callback, tiempoDeEspera) => {
  let timeout 
  return (...args) => {
    clearTimeout(timeout)
    timeout = setTimeout(()=> callback(...args), tiempoDeEspera)
  }
}

Our debounce function returns a function, which will receive any number of arguments (…args).

This function uses a closure to access the variable timeout. What is timeout? timeout is a setTimeout function, which schedules the execution of our callback for later execution.

But now pay attention to the clearTimeout. Every time we call the debounce function it will clear any scheduled function, so the only way for our callback to run is to wait for the time we passed as an argument.

Throttle

The throttle pattern sets a waiting time during which no more functions can be called again. Unlike the bounce pattern, the timeout is not reset if we try to call the function again.

Simplified diagram of the throttling pattern

Explanation of throttle

The code for throttle in javascript looks like this.

const throttle = (callback, delay) => {
  let timeout
  return (...args) => {
    if (timeout !== undefined) {
      return
    }

    timeout = setTimeout(() => {
      timeout = undefined
    }, delay)

    return callback(...args)
  }
}

The throttle function returns a function that will have two sides depending on the timeout status:

  • timeout is defined: this means that a function is already scheduled for execution, in this case the function does nothing, i.e. it blocks the execution of new functions by means of an empty return.
  • timeout is not defined: if timeout is not defined, we create a setTimeout and assign it to the timeout variable. This function, once its execution time has elapsed, will remove itself from the timeout variable. Subsequently, and to finish, we execute the callback function.

Other resources on debounce and throttling

Eduardo Zepeda
Web developer and GNU/Linux enthusiast always learning something new. I believe in choosing the right tool for the job and that simplicity is the ultimate sophistication. I'm under the impression that being perfect is the enemy of getting things done. I also believe in the goodnesses of cryptocurrencies outside of monetary speculation.
Read more