@toolbuilder/ring-buffer
Version:
Replace your slow Array based fixed length ring buffer, or circular queue, with ring-buffer. RingBuffer is roughly 10x faster, and is a drop in replacement.
73 lines • 2.42 kB
TypeScript
/**
* 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.
*/
export class RingBuffer {
/**
* Constructs a RingBuffer with fixed maximum capacity.
* @param {Number} capacity - maximum number of values in the buffer
*/
constructor(capacity: number);
capacity: number;
_buffer: any[];
_first: number;
length: number;
/**
* Empties the ring buffer.
*/
clear(): void;
/**
* Returns the value at the back of the buffer.
* @returns {any} - the back of the buffer, or `undefined` if empty
*/
back(): any;
/**
* Returns the value at the front of the buffer.
* @returns {any} - the front of the buffer, or `undefined` if empty
*/
front(): any;
/**
* Pushes a value onto the back of the buffer. If length === capacity,
* the value at the front of the buffer is discarded.
* @param {any} value - value to push
* @returns {Number} - the current length of the buffer
*/
push(value: any): number;
/**
* 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 {any} the value removed from the back of the buffer
* or `undefined` if empty.
*/
pop(): any;
/**
* 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 {any} the value removed from the front of the buffer
* or `undefined` if empty.
*/
shift(): any;
/**
* Pushes a value on the front of the buffer. If length === capacity,
* the value at the back is discarded.
* @param {any} value - to push onto the front
* @returns {Number} - the current length of the buffer
*/
unshift(value: any): number;
get _last(): number;
_right(): void;
_left(): void;
/**
* Iterator that goes from front to back.
* @returns {Generator} - iterates from front to back
*/
[Symbol.iterator](): Generator;
}
//# sourceMappingURL=ringbuffer.d.ts.map