{ } qjs-modules

list

Source: quickjs-list.c — module exports List, ListIterator, ListNode

A doubly-linked list with an Array-like API plus node-level access.

Every method here is O(1), or O(k) bounded by an explicit argument (a count, or the size of the Node-reference range you pass in) — never a hidden O(n) full-list walk. Search/lookup and functional-iteration convenience methods that Array offers (includes, indexOf, lastIndexOf, find/findLast/findIndex/findLastIndex, every, some, filter, forEach, map, reduce, reduceRight, toReversed, and index-based at()) are deliberately not provided: a linked list can't do any of these without visiting every node, and unlike sort/clear/reverse (below) that cost isn't inherent to the operation's definition — it's just what a linked list happens to be bad at. Use values()/keys()/entries() (each step O(1)) to write the equivalent loop yourself, with the O(n) cost visible in your own code rather than hidden inside a single call.

List

new List([iterable])   // length 1

Structural methods

MethodArgsDescription
clear()0Removes all nodes. O(n): must free every node.
begin() / end()0Iterators to the first / past-the-last node.
rbegin() / rend()0Reverse iterators.
erase(it) / erase(start, end)1–2Removes one node, or a Node-reference range.
insert(after, ...values)1+Inserts after a Node reference (or the tail if omitted).
insertBefore(before, ...values)1+Inserts before a Node reference.
unique()0Removes consecutive duplicates. O(n): must compare every adjacent pair once.
merge(other)1Merges another (sorted) list's values in; other itself is left unmodified. O(n+m): must visit every element of both.

Array-style methods

MethodArgsDescription
push / pop / unshift / shift0–1Add/remove at the ends.
concat(...args)0+Splices each List argument's entire contents in directly (O(1) per List), consuming this list and every List argument (they end up empty) — a deliberate departure from Array.prototype.concat's non-mutating semantics, in exchange for genuine O(1) cost regardless of list size. A non-List iterable argument is still appended in O(m), m = its own length, since there's no linked structure to splice.
slice(start, end)2Copies the Node-reference range [start, end); cost is O(k), k = range size.
reverse()0In-place reversal. O(n): must relink every node once.
splice(start, end, ...items)2+Removes the Node-reference range [start, end) (O(1) pointer splice) and inserts items in its place (O(i)).
fill(value, start, end)3Fills the Node-reference range [start, end) with value; cost is O(k), k = range size.
rotate(node)1Repositions the list's start to node in O(1) (moves only the sentinel, not the elements).
sort([cmp])0Sort in place. O(n log n): inherent to sorting, like Array.prototype.sort.

Iteration

values(), keys(), entries() (and [Symbol.iterator] = values).

Properties

PropertyKindDescription
lengthgetterNumber of elements (enumerable).
addressgetterNative address of the list.

Static functions

FunctionArgsDescription
from(iterable)1Builds a list from an iterable.
of(...items)0Builds a list from arguments.
isList(value)1Type predicate.

ListIterator

new ListIterator(...)   // length 1
MemberArgsKindDescription
next()0methodIterator step.
equals(other)1methodCompares two iterators.
copy()0methodClones the iterator.
isAccessible()0methodWhether the iterator points at a live node.
container—getterThe owning list.
type—getterIteration kind.

ListNode

new ListNode(value)   // length 1
MemberArgsKindDescription
equals(other)1methodNode identity comparison.
valueOf()0methodReturns the node value.
prev / next—getterNeighboring nodes.
linked—getterWhether the node is in a list.
sentinel—getterWhether it is the list sentinel.
value—getter/setterThe stored value.
address—getterNative address.