Skip to content

Latest commit

 

History

History
129 lines (78 loc) · 3.1 KB

dynamic-ring-buffer.md

File metadata and controls

129 lines (78 loc) · 3.1 KB

Table of Contents

DynamicRingBuffer

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, default 100)
  • capacity number maximum number of values in the buffer (optional, default Number.MAX_SAFE_INTEGER)

length

Property provides the current number of elements in the buffer - it's length.

clear

Empties the ring buffer.

Returns void

back

Returns the value at the back of the buffer.

Returns Type the back of the buffer, or undefined if empty

front

Returns the value at the front of the buffer.

Returns Type the front of the buffer, or undefined if empty

push

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

pop

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.

shift

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.

unshift

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

Iterator that goes from front to back.

Returns IterableIterator<Type> iterates from front to back