Overview
Collects values from the source Observable into arrays of a specified maximum size. When the buffer reaches thebufferSize, it emits the array and starts a new buffer. Optionally, you can control when new buffers start using the startBufferEvery parameter.
Type Signature
Parameters
The maximum number of values to buffer before emitting. Once the buffer contains this many values, it is emitted and cleared.
The interval at which to start new buffers. If not provided or
null, a new buffer starts immediately after the previous one emits. If set to a value less than bufferSize, buffers will overlap. If greater, some values will be skipped between buffers.Returns
A function that returns an Observable of arrays, where each array contains at most
bufferSize values from the source.Usage Examples
Basic Example: Buffer Every N Items
Overlapping Buffers with startBufferEvery
Skipping Values Between Buffers
Marble Diagram
bufferCount(3)
bufferCount(2, 1) - Overlapping
bufferCount(2, 3) - Skipping
Common Use Cases
- Batch Processing: Process items in fixed-size batches (e.g., bulk database inserts)
- Pagination: Create pages of results from a stream of items
- Sliding Windows: Use overlapping buffers to analyze trends
- Rate Limiting: Process items in groups to avoid overwhelming downstream systems
- Pair-wise Operations: Use
bufferCount(2, 1)to compare consecutive values
When the source completes, any incomplete buffer (containing fewer than
bufferSize items) is still emitted.Advanced Example: Batch API Requests
Related Operators
- buffer - Buffer based on a notifier Observable
- bufferTime - Buffer based on time intervals
- bufferToggle - Buffer with opening and closing signals
- bufferWhen - Buffer using a factory function
- windowCount - Like bufferCount, but emits Observables instead of arrays
- pairwise - A specialized form of
bufferCount(2, 1)
