Queue

cispy/buffers~ Queue

Source:

A general purpose, highly efficient JavaScript queue. It is backed by a JavaScript array, but it does not use unshift to take elements off the array because unshift causes elements to be copied every time it's used. Instead, a pointer is maintained that keeps track of the location of the next element to be dequeued, and when that dequeue happens, the pointer simply moves. When the empty space at the head of the array gets large enough, it's removed by a single slice operation.

Putting elements into the queue is just done with a basic push, which is highly efficient.

This type of queue is possible in JavaScript because JS arrays are resizable. In languages with fixed-size arrays, a resizing operation would have to be run each time the queue fills.

Members

(readonly) count :number

Source:

Returns the number of elements stored in the queue. This may or may not equal the length of the backing store.

Type:
  • number

(readonly) empty :boolean

Source:

Returns true if the queue is empty.

Type:
  • boolean

Methods

dequeue() → {*}

Source:

Removes an item from the queue and returns that item. If the removal causes the amount of empty space at the head of the backing store to exceed a threshold, that empty space is removed.

Returns:

The oldest stored item in the queue.

Type
*

enqueue(item)

Source:

Adds an item to the queue.

Parameters:
Name Type Description
item *

The value being added to the queue.

filter(fn)

Source:

Filters out any item in the queue that does not cause the supplied predicate function to return true when passed that item. This is not exactly a general purpose queue operation, but we need it with channels that will occasionally want to get rid of inactive handlers.

Parameters:
Name Type Description
fn function

The predicate function that determines whether an element remains in the queue.

peek() → {*}

Source:

Returns the next item in the queue without removing it.

Returns:

The oldest item stored in the queue.

Type
*