RingBuffer implements classic fixed length ring buffer, or circular queue.
The methods match the Array signature for push, pop, unshift, and shift.
For buffer operation either use push/shift together, or unshift/pop together.
RingBuffer is substantially faster than an Array for this use case.
capacityNumber maximum number of values in the buffer
Empties the ring buffer.
Returns the back of the buffer, or undefined if empty
Returns 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.
valueany value to push
Returns the current length of the 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 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 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.
valueany to push onto the front
Returns the current length of the buffer.
Iterator that goes from front to back.
Returns Generator iterates from front to back