24 Jul 2024
1 min

debounceTime – RxJS Reference

debounceTime

debounceTime delays the values emitted by a source for the given due time. If within this time a new value arrives, the previous pending value is dropped and the timer is reset. In this way debounceTime keeps track of most recent value and emits that most recent value when the given due time is passed.

The operator works in the following way:

  1. when new value arrives schedule a new interval
  2. remember the value and discard old ones if exists
  3. when the interval ends, emit the value
  4. if new value comes before the interval ends, run step 1 again

By default it uses setInterval through AsyncScheduler under the hood for scheduling.

Usage

This operator is mostly used for events that can be triggered tens or even hundreds of times per second. The most common examples are DOM events such as scrolling, mouse movement, and keypress. When using debouceTime you only care about the final state. For example, current scroll position when a user stops scrolling, or a final text in a searchbox after a user stops typing in characters. In effect, using the operator allows grouping multiple sequential events into a single one and hence execute the callback just once. This can greatly improve performance.

Common scenarios for a debounce are resize, scroll, and keyup/keydown events. In addition, you should consider wrapping any interaction that triggers excessive calculations or API calls with a debounce.

Here’s an example of using debounceTime on an input:

const inputElement = document.createElement('input');
document.body.appendChild(inputElement);

fromEvent(inputElement, 'input')
   .pipe(
       debounceTime(500),
       map((event: any) => event.target.value)
   ).subscribe(val => console.log(val));

Playground

Additional resources

See also

Frequently asked questions

How do I debounce user input in RxJS?

Use the debounceTime(dueTime) operator in a pipe on the source observable, such as fromEvent(input, 'input'). It waits for the given due time, for example 500 ms, and emits only the most recent value once no new value arrives in that window. Earlier pending values are dropped and the timer resets on each new event, so a search box only fires its request after the user stops typing.

What is the difference between debounceTime and throttleTime in RxJS?

debounceTime emits the latest value only after the source has been silent for the given due time, so it keeps the final state of a burst of events. throttleTime emits the first value and then ignores events for the duration, which suits rate limiting. Use debounceTime for scroll positions or search text where only the final value matters, and throttleTime when you need periodic updates during continuous activity.

Why does debounceTime use setInterval under the hood?

By default debounceTime schedules its timer through the AsyncScheduler, which relies on setInterval. Each new value schedules a new interval, discarding the previously remembered value, and the last value is emitted when the interval ends. You can pass a different scheduler as the second argument to debounceTime if you need to control timing, for example in tests.

When should I use debounceTime for DOM events like scroll or keyup?

Use debounceTime for DOM events that can fire tens or hundreds of times per second, such as scroll, resize, keyup, and keydown, when you only care about the final state. It groups sequential events into one emission, so the callback runs once. Also consider it for any interaction that triggers heavy calculations or API calls, since it reduces unnecessary work.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.