DynamicRingBuffer implements classic fixed length ring buffer, or circular queue. Memory is dynamically allocated and deallocated in chunks as the buffer grows and shrinks. This allows the buffer to have a large capacity to handle data bursts, while minimizing memory use during normal operation.
When the buffer length reaches capacity, the oldest values are quietly thrown away. The methods match the Array signature for push, pop, unshift, and shift. For buffer operation either use push/shift together, or unshift/pop together.
DynamicRingBuffer allocates and deallocates internal buffers as the length increases and decreases to tolerate rare bursts of data, while using memory frugally when data flows slow.
Parameters:
chunkSize
number buffer will grow and shrink in chunkSize increments (optional, default100
)capacity
number maximum number of values in the buffer (optional, defaultNumber.MAX_SAFE_INTEGER
)
Property provides the current number of elements in the buffer - it's length.
Empties the ring buffer.
Returns void
Returns the value at the back of the buffer.
Returns Type the back of the buffer, or undefined
if empty
Returns the value at the front of the buffer.
Returns Type the front of the buffer, or undefined
if empty
Pushes a value onto the back of the buffer. If length === capacity, the value at the front of the buffer is discarded.
Parameters:
value
Type value to push
Returns number current length of buffer
Removes a value from the back of the buffer and returns it. The newly empty buffer location is set to undefined to release any object references.
Returns Type the value removed from the back of the buffer
or undefined
if empty.
Removes a value from the front of the buffer and returns it. The newly empty buffer location is set to undefined to release any object references.
Returns Type the value removed from the front of the buffer
or undefined
if empty.
Pushes a value on the front of the buffer. If length === capacity, the value at the back is discarded.
Parameters:
value
Type to push onto the front
Returns number current length of buffer
Iterator that goes from front to back.
Returns IterableIterator<Type> iterates from front to back