Queues elements at both ends in O(1) amortized time, over array and Float64Array ring buffers, with an optional fixed capacity that turns the queue into a sliding window
over a plain array ring buffer, for elements of any
type except undefined. The type parameter excludes undefined, so
arrayDeque<number | undefined>() fails to compile; store null, or a
sentinel of your own, for a missing value.
@throws ― {DequeInvalidInputError} With code INVALID_CAPACITY when
options.capacity is not Infinity or a positive safe integer no larger than
DEQUE_MAX_CAPACITY (Infinity is accepted), INVALID_VALUE when
options.items holds undefined, or MAX_CAPACITY_EXCEEDED when
options.items holds more elements than the layout can store.
over a plain array ring buffer, for elements of any
type except undefined. The type parameter excludes undefined, so
arrayDeque<number | undefined>() fails to compile; store null, or a
sentinel of your own, for a missing value.
@throws ― {DequeInvalidInputError} With code INVALID_CAPACITY when
options.capacity is not Infinity or a positive safe integer no larger than
DEQUE_MAX_CAPACITY (Infinity is accepted), INVALID_VALUE when
options.items holds undefined, or MAX_CAPACITY_EXCEEDED when
options.items holds more elements than the layout can store.
@returns ― The element evicted from the front when the deque was at
capacity; otherwise undefined.
@throws ― {DequeInvalidInputError} With code INVALID_VALUE when the layout
cannot store the value, or MAX_CAPACITY_EXCEEDED when growing would pass
DEQUE_MAX_CAPACITY or the runtime refuses the allocation. The
deque is unchanged after either.
pushBack('render')
constfrontier:Deque<string>
frontier.
Deque<string>.popFront: () => string
Remove and return the element at the front. O(1).
@throws ― {DequeInvalidInputError} With code EMPTY when the deque is empty.
popFront() // 'parse'
constfrontier:Deque<string>
frontier.
Deque<string>.popFront: () => string
Remove and return the element at the front. O(1).
@throws ― {DequeInvalidInputError} With code EMPTY when the deque is empty.
popFront() // 'layout'
constfrontier:Deque<string>
frontier.
Deque<string>.popFront: () => string
Remove and return the element at the front. O(1).
@throws ― {DequeInvalidInputError} With code EMPTY when the deque is empty.
of numbers over a Float64Array ring buffer: eight
bytes per slot and no per-element allocation. NaN and -0 are stored as
given.
@throws ― {DequeInvalidInputError} With code INVALID_CAPACITY when
options.capacity is not Infinity or a positive safe integer no larger than
DEQUE_MAX_CAPACITY (Infinity is accepted), INVALID_VALUE when
options.items holds anything but numbers, or MAX_CAPACITY_EXCEEDED
when options.items holds more elements than the runtime will allocate
storage for.
of numbers over a Float64Array ring buffer: eight
bytes per slot and no per-element allocation. NaN and -0 are stored as
given.
@throws ― {DequeInvalidInputError} With code INVALID_CAPACITY when
options.capacity is not Infinity or a positive safe integer no larger than
DEQUE_MAX_CAPACITY (Infinity is accepted), INVALID_VALUE when
options.items holds anything but numbers, or MAX_CAPACITY_EXCEEDED
when options.items holds more elements than the runtime will allocate
storage for.
float64ArrayDeque({
DequeOptions<number>.capacity?: number |undefined
The most elements the deque holds. A push onto a full deque evicts the
element at the opposite end and returns it, which makes a bounded deque a
sliding window. Must be Infinity or a positive safe integer no larger
than
DEQUE_MAX_CAPACITY
. Omit it, or pass Infinity, for a deque
that grows without bound; that is the value
Deque.capacity
reports.
capacity: 60 })
let
let sum:number
sum=0
function
functionobserve(value:number):number
observe(
value: number
value:number):number {
let sum:number
sum+=
value: number
value
const
constevicted:number|undefined
evicted=
constwindow:Deque<number>
window.
Deque<number>.pushBack: (value:number) => number |undefined
Add an element at the back. O(1) amortized.
@returns ― The element evicted from the front when the deque was at
capacity; otherwise undefined.
@throws ― {DequeInvalidInputError} With code INVALID_VALUE when the layout
cannot store the value, or MAX_CAPACITY_EXCEEDED when growing would pass
DEQUE_MAX_CAPACITY or the runtime refuses the allocation. The
deque is unchanged after either.
pushBack(
value: number
value) // undefined until the window is full
if (
constevicted:number|undefined
evicted!==
var undefined
undefined)
let sum:number
sum-=
constevicted:number
evicted
return
let sum:number
sum/
constwindow:Deque<number>
window.
Deque<number>.size: number
Number of elements held. O(1). The deque is empty when this is 0.
any element is not a number, such as an object, a string, or null
one boxed slot per element and a slot write on every pop to release it; on the pinned 100,000-element first-in first-out bench it takes 2.6 times as long as float64-array
float64ArrayDeque
kind float64-array
every element is a number, including NaN and -0
stores numbers only and rejects anything else with INVALID_VALUE; eight bytes per slot whether or not the value is an integer
Double-ended queue over a plain array ring buffer for any element type except undefined; an optional capacity evicts from the opposite end and returns the evicted element.
Double-ended queue of numbers over a Float64Array ring buffer, eight bytes per slot; an optional capacity evicts from the opposite end and returns the evicted element.
insert or remove items in the middle, or overwrite an item by index
Array with splice, or a linked list
a stack used at one end only
Array push and pop, which are already O(1)
a bounded forward-only scan whose maximum size is known, such as a breadth-first walk over n vertices
a pre-sized Int32Array or Float64Array with head and tail cursors, indexed by two integers that only move forward; it never wraps or grows, so a deque only adds cost
Use knotkit/deque for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/deque'.
Exports:
- `arrayDeque` → `Deque<A>`: Double-ended queue over a plain array ring buffer for any element type except undefined; an optional capacity evicts from the opposite end and returns the evicted element.
- `float64ArrayDeque` → `Deque<number>`: Double-ended queue of numbers over a Float64Array ring buffer, eight bytes per slot; an optional capacity evicts from the opposite end and returns the evicted element.
Pick the layout by these rules:
- `arrayDeque` (kind 'array') when any element is not a number, such as an object, a string, or null
- `float64ArrayDeque` (kind 'float64-array') when every element is a number, including NaN and -0
Use something else when:
- always take the smallest or largest item rather than the oldest or newest → knotkit/heap
- evict entries by key, recency of use, or total weight → knotkit/cache
- insert or remove items in the middle, or overwrite an item by index → Array with splice, or a linked list
- a stack used at one end only → Array push and pop, which are already O(1)
- a bounded forward-only scan whose maximum size is known, such as a breadth-first walk over n vertices → a pre-sized Int32Array or Float64Array with head and tail cursors, indexed by two integers that only move forward; it never wraps or grows, so a deque only adds cost
Errors are DequeInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/deque.md
Types: node_modules/knotkit/deque/ and https://knotkit.jkomyno.dev/reference/knotkit/deque/