An ordered collection of values with fast random access, push, pop, shift, and unshift, but slow to splice.
A double ended queue is backed by a circular buffer, which cuts down on garbage collector churn. As long as the queue is stable, meaning values are added and removed at roughtly the same pace, the backing store will not create new objects. The store itself is an object with numeric indexes, like an array. The indexes of the deque are offset from the indexes within the circular buffer, and values spill over from the end of the buffer back to the beginning. As values are removed by way of shifting, it makes room for values by way of pushing.
Deques have constants maxCapacity
and minCapacity
.
The number of items in this collection.
Adds values to the end of a collection.
Removes a value from the end of a collection, and returns that value.
Removes a value from the beginning of a collection, and returns that value.
Adds values to the beginning of a collection.
Returns the value at the beginning of a collection, the value that would be returned by shift()
.
Replaces the value at the beginning of a collection, the value that would be returned by shift()
.
Returns the value at the end of a collection, the value that would be returned by pop()
.
Replaces the value at the end of a collection, the value that would be returned by pop()
.
Whether an equivalent value exists in this collection.
Returns whether an equivalent value exists in this collection.
Retrieves the equivalent value from the collection.
Retrieves the equivalent value from this collection.
Adds a value to a collection.
Copies values or entries from another collection into this collection, and then returns this.
Deletes every value or every value for each key. Returns the number of successful deletions.
Deletes all of the values in the collection.
Returns the position of a value, or -1 if the value is not found.
Returns the last position of a value, or -1 if the value is not found.
Returns the position of a value, or -1 if the value is not found.
Returns the last position of a value, or -1 if the value is not found.
Finds the first equivalent value.
Finds the first equivalent value.
Finds the last equivalent value, searching from the right.
Finds the last equivalent value, searching from the right.
Iterates every value in this collection.
Calls the callback for each entry in the collection.
Returns an array of the respective return values of a callback for each entry in this collection.
Returns an array with each value from this collection that passes the given test.
Aggregates every value in this collection with the result collected up to that index.
Aggregates every value in this collection, from right to left.
Returns an array of [key, class] entries where every value from the collection is placed into the same equivalence class if they return the same key through the given callback.
Returns whether any entry in this collection passes a given test.
Returns whether every entry in this collection passes a given test.
Returns whether any value in the collection is truthy.
Returns whether all values in the collection are truthy.
Returns one, arbitrary value from this collection, or undefined if there are none.
Returns the only value in this collection, or undefined if there is more than one value, or if there are no values in the collection.
Returns a sorted array of the values in this collection.
Returns a copy of this collection with the values in reverse order.
Returns a string of all the values in the collection delimited by the given string.
Returns the sum of all values in this collection.
Returns the arithmetic mean of the collection, by computing its sum and the count of values and returning the quotient.
Returns the smallest value in this collection.
Returns the largest value in this collection.
Returns an array of the respective values in this collection and in each collection provided as an argument.
Returns an array of [index, value] entries for each value in this collection, counting all values from the given index.
Returns a new collection of the same type containing all the values of itself and the values of any number of other iterable collections in order.
Assuming that this is a collection of collections, returns a new collection that contains all the values of each nested collection in order.
Returns an array of each value in this collection.
Returns an object with each property name and value corresponding to the entries in this collection.
Used by JSON.stringify
to create a JSON representation of the collection.
Returns whether this collection is equivalent to the given collection.
Compares two values and returns a number having the same relative value to zero.
Creates a deep replica of this collection.
Creates a shallow clone of this collection.
An internal method of Deque
that will grow the backing store if necessary.
An implementation detail of a Deque
that will increase the size of its backing store.
Adds a listener for when values are added or removed at any position.
Unregisters a range change listener provided by addRangeChangeListener.
Informs range change listeners that values were removed then added at an index.
Adds a listener for before values are added or removed at any position.
Unregisters a range change listener provided by addBeforeRangeChangeListener or addRangeChangeListener with the beforeChange
flag.
Informs range change listeners that values will be removed then added at an index.
var Deque = require("collections/deque");
var deque = new Deque([1, 2, 3]); deque.unshift(0); deque.push(4); deque.toArray();