24 Jul 2024
2 min

bufferTime – RxJS Reference

bufferTime

bufferTime collects values emitted from the source observable into cache without passing them down to an observer until the specified time interval elapses (buffering). The buffer then sends the cached values as a group, resets and starts buffering again until time interval elapses once again.

This operator is similar to buffer, but instead of using an observable for indication when to flush the buffer, it uses a time interval.

One interesting behavior of the bufferTime operator is that it can send an empty set of values to the observer if the time interval elapses while the cache is empty.

The operator works in the following way:

  1. Subscribe to a source observable
  2. Start a time span interval
  3. When a new value arrives from a source observable, put it in a cache
  4. When an interval ends, send all values from the cache to the observer, even if the cache is empty
  5. Continue from step 2
  6. Once the source completes, send the complete notification to the observer
  7. If the source observable throws an error, send the error notification to the observer.

There’s also a special case of this operator that allows ignoring certain values. To do that, the operator must be used with the creation interval parameter like this:

const bufferCreationInterval = 1000;
const bufferTimeSpan = 500;
interval(200).pipe(
   take(10),
   // this setup will ignore values 2, 3, 7, 8
   bufferTime(bufferTimeSpan, bufferCreationInterval)
).subscribe((v) => console.log(v));

With the creation interval, the operator works in the following way:

  1. Subscribe to a source observable
  2. Start both a time span and a creation intervals
  3. When a new value arrives from a source observable, put it in a cache if the time span interval is running, ignore otherwise
  4. When a time span interval ends, send all values from the cache to the observer, even if the cache is empty
  5. When a creation interval ends, continue from step 2
  6. Once the source completes, send the complete notification to the observer
  7. If the source observable throws an error, send the error notification to the observer.

Usage

bufferTime is often used when the batching technique is required. For example, sometimes you need to repeatedly perform a very expensive operation within a very small time frame, such as re-rendering a DOM tree on updates from a stream, batching can help collect updates and render them at once.

Here’s an example of using bufferTime to emit array of the most recent clicks:

const clicks = fromEvent(document, 'click');
const buffered = clicks.pipe(bufferTime(1000));
buffered.subscribe(x => console.log(x));

Playground

Additional resources

See also

Frequently asked questions

How do I batch RxJS emissions over a fixed time window with bufferTime?

Use bufferTime(timeSpan) in a pipe to collect source values for each time span and emit them as one array. For example, clicks.pipe(bufferTime(1000)) emits an array of the clicks that happened in each one-second window. The operator sends the cached values even when the cache is empty, so subscribers can receive empty arrays when no values arrived during the interval.

What is the difference between bufferTime and buffer in RxJS?

bufferTime flushes its cache on a fixed time interval, while buffer flushes whenever a separate notifier observable emits. Use bufferTime when batching should happen at regular time intervals, and buffer or bufferWhen when the flush should be triggered by another stream or event. The article presents bufferTime as the time-based counterpart to buffer.

Why does bufferTime emit empty arrays in RxJS?

bufferTime sends the cached values when each time span ends, even if the cache is empty, so it can emit an empty array when no source values arrived during that interval. This is the documented behavior, not an error. If you need to skip empty batches, filter them downstream with the filter() operator, for example filter(batch => batch.length > 0).

How do I ignore some values with bufferTime using a creation interval?

Pass a second argument, the buffer creation interval, as in bufferTime(bufferTimeSpan, bufferCreationInterval). A new buffer starts every creation interval and collects values only while the time span is running, so values arriving outside that span are dropped. In the article's example, interval(200) with a 500 ms span and a 1000 ms creation interval ignores values 2, 3, 7 and 8.

Share this post

Sign up for our newsletter

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