Snowcone field notes 01 / 03

A JavaScript queue with Snowcone: FIFO without Array.shift()

For an in-memory FIFO queue in Node.js, Snowcone provides Queue with enqueue(), dequeue(), peek(), size, and isEmpty(). Items leave in the order they arrived. Its head-index implementation avoids removing the first array element on every dequeue.

Package note: This guide describes version 0.2.0 and includes native alternatives where they are useful.

Install and try a FIFO queue

Use Node.js 20 or later and ECMAScript modules. Save the examples as .mjs files, or set "type": "module" in your project’s package.json.

npm install [email protected]

Pinning the version makes these examples reproducible. Check npm for newer releases and compare their API before upgrading.

import { Queue } from "snowcone";

const queue = new Queue();
queue.enqueue("first job");
queue.enqueue("second job");

console.log(queue.peek());    // first job
console.log(queue.dequeue()); // first job
console.log(queue.size);      // 1
console.log(queue.dequeue()); // second job
console.log(queue.isEmpty()); // true
console.log(queue.dequeue()); // undefined

FIFO means first in, first out: the first job added is the first job removed. peek() looks at that next job without removing it. An empty peek() or dequeue() returns undefined.

Why use a queue instead of an array?

A JavaScript array is enough for a tiny queue. A common approach adds with push() and removes with shift(). Removing from the front changes the indexing of the remaining elements, so its cost can depend on the array and engine. Snowcone instead advances a head index and periodically compacts the backing array.

That makes the data structure useful for algorithms that repeatedly take the oldest item, such as breadth-first search or a process-local list of pending jobs. It also makes the FIFO intent explicit when reading the code.

Queue API and costs

MemberBehaviorCost model
enqueue(item)Add an item to the back; returns undefined.Amortized O(1)
dequeue()Remove the oldest item, or return undefined if empty.Amortized O(1); a compaction can be O(n)
peek()Read the oldest item without removing it.O(1)
sizeRead the number of queued items; a getter, not a method.O(1)
isEmpty()Check whether the queue has no items.O(1)

These costs describe the implementation’s array/head-index model, not a JavaScript-engine timing guarantee or a benchmark. Storage grows with queued items; the private array may also temporarily contain consumed slots.

How the head-index design works

In version 0.2.0, enqueue() appends to a private array. dequeue() reads the item at the head, clears that slot to release the queue’s reference, and advances the head. Once the queue empties, it resets the array and head.

While nonempty, it copies the remaining suffix when the head has reached at least 1,024 slots and consumed at least half the backing array. A single such dequeue is linear in the remaining items; spreading that copying over many dequeues is why the cost is described as amortized, rather than worst-case constant time.

When to use it—and when not to

Use this queue when you want a small synchronous FIFO container inside one Node.js process. It does not run jobs, limit concurrency, persist data, retry work, or communicate between processes. For durable background jobs, choose a job-queue system with those guarantees.

The constructor starts empty; initialize it by calling enqueue(). There is no built-in capacity limit or iterator. If you enqueue undefined, use isEmpty() or size to distinguish that stored value from an empty result. The published root module imports a Node.js file helper, so these examples target Node.js rather than claiming browser compatibility.

Common questions

Is Queue built into JavaScript?

JavaScript has arrays, but no standard global Queue class. Snowcone supplies the FIFO class shown here.

Does enqueue return the new size?

No. In 0.2.0 it returns undefined. Read queue.size separately.

Is this faster than every array queue?

There is no benchmark here supporting that claim. The concrete distinction is its head-index design and explicit FIFO API. Measure your own workload if throughput is the deciding factor.

Sources and version

Examples were checked against the published [email protected] package. Implementation details refer to the linked source snapshot, not a promise about future versions.