24 Jul 2024
2 min

throttleTime – RxJS Reference

throttleTime

throttleTime delays the values emitted by a source 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, throttleTime guarantees the values will be emitted regurarly, but not more often than the configured interval.

This operator takes an optional configuration parameter that can change the behavior of the operator: {leading: boolean, trailing: boolean}. The default settings are {leading: true, trailing: false}.

For the default configuration {leading: true, trailing: false} the operator works in the following way:

  1. when new value arrives schedule a new interval
  2. emit the value to the observer
  3. if a new value comes before the interval ends, ignore it

For the configuration {leading: true, trailing: true} the operator works in the following way:

  1. when new value arrives schedule a new interval
  2. emit the value to the observer
  3. if a new value comes before the interval ends, keep it and override the existing one
  4. once the interval ends, emit the value to the observer

For the configuration {leading: false, trailing: true} the operator works in the following way:

  1. when new value arrives schedule a new interval
  2. keep the value
  3. if a new value comes before the interval ends, keep it and override the existing one
  4. once the interval ends, emit the value to the observer

By default the operator 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, 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 throttleTime on an input:

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

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

Playground

Leading – true, Trailing – false

Leading – true, Trailing – true

Leading – false, Trailing – true

Additional resources

See also

Frequently asked questions

What is the difference between throttleTime and debounceTime in RxJS?

throttleTime emits values at a regular rate, no more often than the configured interval, while debounceTime waits until the source has been silent for the given duration before emitting. Because throttleTime guarantees periodic emissions, it suits continuous events like scrolling or resizing. By default, throttleTime uses {leading: true, trailing: false}, so it emits the first value immediately and ignores values that arrive during the interval.

How do I use throttleTime with leading and trailing options in RxJS?

Pass a configuration object as the second argument, such as throttleTime(500, asyncScheduler, { leading: true, trailing: true }). With leading: true the first value is emitted immediately, and with trailing: true the most recent value received during the interval is emitted when the interval ends. Setting leading: false, trailing: true emits only the last value after each interval. The default is {leading: true, trailing: false}.

Why does throttleTime ignore values that arrive during the interval?

With the default {leading: true, trailing: false} configuration, throttleTime emits a value, starts an interval, and drops any values that arrive before that interval ends. This is the intended behavior for rate limiting, but it means the last value may never be emitted. To keep the final value, set trailing: true so the operator emits the latest held value once the interval completes.

When should I use throttleTime for DOM events like scroll or input in Angular?

Use throttleTime for events that fire tens or hundreds of times per second, such as scrolling, resizing, mouse movement, or keypresses, when the handler does heavy work or triggers API calls. Wrapping the event stream with fromEvent() and throttleTime() reduces handler executions without disrupting the user experience. In an Angular component, convert the stream to a signal or subscribe within the lifecycle so the subscription is cleaned up properly.

Is throttleTime scheduled with setInterval by default in RxJS?

Yes, by default throttleTime uses setInterval through the AsyncScheduler for scheduling the interval. You can pass a different scheduler as the second argument if you need different timing behavior. The operator's configuration parameter, {leading: boolean, trailing: boolean}, controls whether values are emitted at the start of the interval, at the end, or both.

Share this post

Sign up for our newsletter

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