24 Jul 2024
1 min

buffer – RxJS Reference

buffer

buffer collects values emitted from the source observable into cache without passing them down to an observer until the notifier observable emits (buffering). The buffer then sends the cached values as a group, resets and starts buffering again until the provided observable emits once more.

One interesting behavior of the buffer operator is that it can send empty set of values to the observer if the notifier observable emits a value and the cache is empty.

The operator works in the following way:

  1. Subscribe to a source observable
  2. Subscribe to a notifier observable
  3. When a new value arrives from a source observable, put it in a cache
  4. When a notifier observable emits a value, send all values from the cache to the observer, even if the cache is empty
  5. Once the source or the notifier observable completes, send the complete notification to the observer
  6. If any of the source observables throws an error, send the error notification to the observer.

buffer completes if either source observable or notifier observable completes.

Usage

buffer 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 buffer to emit array of most recent interval events on every click:

const clicks = fromEvent(document, 'click');
const intervalEvents = interval(1000);

const buffered = intervalEvents.pipe(buffer(clicks));

buffered.subscribe(x => console.log(x));

Playground

Additional resources

See also

Frequently asked questions

How do I batch RxJS values until another observable emits?

Use the buffer() operator and pass a notifier observable to it, such as intervalEvents.pipe(buffer(clicks)). The source values are collected in an internal cache and only sent to the subscriber as an array when the notifier emits. The cache is then reset and buffering starts again until the next notifier emission.

Why does buffer() emit an empty array in RxJS?

buffer() emits an empty array when the notifier observable emits while the cache is empty. This happens because the operator sends all cached values on every notifier emission, even if none arrived since the last one. If you want to skip these empty emissions, filter the output, for example with filter(arr => arr.length > 0).

What is the difference between buffer() and bufferTime() in RxJS?

buffer() flushes its cache whenever a separate notifier observable emits, while bufferTime() flushes on a fixed time interval. Use buffer() when batching should follow user actions or other events, such as clicks. Use bufferTime() when you need batches of a fixed duration. The related bufferWhen() takes a function that controls when each buffer closes.

When should I use buffer() to batch updates in an Angular app?

Use buffer() when a stream emits updates faster than you want to process them, such as collecting rapid events and rendering them together. The operator is useful for expensive work like re-rendering a DOM tree on stream updates. Since the batch is released only when the notifier fires, choose a notifier that matches the moment you want the grouped work to run.

Does buffer() complete when the source or notifier observable completes?

Yes, buffer() completes as soon as either the source observable or the notifier observable completes. If the source throws an error, the error is forwarded to the observer. Values still held in the cache at completion are not described as flushed by the operator, so account for that when designing the notifier's lifecycle.

Share this post

Sign up for our newsletter

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