24 Jul 2024
2 min

auditTime – RxJS Reference

auditTime

auditTime delays the values emitted by a source observable for the given due time. Similarly to debounceTime, this operator can be used to control the rate with which the values are emitted to an observer. Unlike debounceTime though, auditTime guarantees the values will be emitted regurarly, but not more often than the configured interval.

auditTime is very similar to throttleTime with the configuration {leading: false, trailing: true}. There’s one difference regarding the handling of the trailing value. If the source observable completes before the interval ends, throttleTime still sends the value to an observer, while auditTime discards it.

The operator works in the following way:

  • when a new value arrives schedule a new interval
  • keep the value
  • if a new value comes before the interval ends, keep it and override the existing one
  • once the interval ends, emit the value to the observer
  • if the source observable completes before the interval ends, discard the value

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

Usage

When you are interested in ignoring values from a source observable for a given amount of time, you can use auditTime. It 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, resizing, mouse movements, and keypress.

For example, if you attach a scroll handler to an element, and scroll that element down to say 5000px, you’re likely to see 100+ events being fired. If your event handler performs multiple tasks (like heavy calculations and other DOM manipulation), you may see performance issues (jank). Reducing the number of times this handler is executed without disrupting user experience will have a significant effect on performance.

In addition, you should consider wrapping any interaction that triggers excessive calculations or API calls with a throttle.

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

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

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

Playground

Additional resources

See also

Frequently asked questions

What is the difference between auditTime and debounceTime in RxJS?

auditTime emits the latest value at regular intervals, while debounceTime emits only after the source has been silent for the given duration. Because auditTime guarantees periodic emissions, it suits high-frequency events like scrolling or resizing, where debounceTime could delay output indefinitely during continuous activity. Both operators control the rate of emitted values but behave differently under constant input.

How is auditTime different from throttleTime with leading false and trailing true?

auditTime behaves like throttleTime configured with {leading: false, trailing: true}, with one difference: when the source completes before the interval ends, throttleTime still emits the pending trailing value, while auditTime discards it. Choose auditTime when you don't want a final value delivered after completion.

How does auditTime work internally in RxJS?

When a value arrives, auditTime schedules an interval and stores the value, replacing any previously stored value if another arrives before the interval ends. Once the interval ends, it emits the stored value. By default it uses setInterval through AsyncScheduler, and if the source completes before the interval ends, the stored value is discarded.

How do I use auditTime to handle input events in RxJS?

Pass fromEvent(inputElement, 'input') into pipe() and add auditTime(500) before mapping the event to its value with map(). This limits emissions to at most one every 500 ms while still sampling the latest input regularly. It is useful for DOM events such as scrolling, resizing, mouse movements, and keypress, which can fire hundreds of times per second.

Share this post

Sign up for our newsletter

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