JavaScript data structures

JavaScript data structures

JavaScript provides arrays, objects, maps, sets, typed arrays, and iterable collections. Custom classes cover structures without a built-in counterpart.

Help-desk ticket system

Use a Map for ticket lookup, a Set for processed events, and a queue for tickets that must be assigned in arrival order. JavaScript does not include a standard priority queue, so a small heap implementation is appropriate when urgent work is required.

const ticketsById = new Map();
const processedEvents = new Set();
const waitingTickets = new Queue();

const ticket = { id: "HD-1042", title: "Cannot sign in", priority: 1 };
ticketsById.set(ticket.id, ticket);

if (!processedEvents.has("evt-8")) {
  processedEvents.add("evt-8");
  waitingTickets.enqueue(ticket.id);
}

The Queue implementation below tracks its front position so it does not repeatedly reindex the array. See Data structure decisions for the reasoning and Big O notation for the cost of each operation.

Arrays

Arrays are ordered, mutable collections. Use them for sequences and stacks.

const names = ["Ada", "Grace"];
names.push("Linus");
const last = names.pop();

Objects

Plain objects are useful for simple string-keyed records. Use Object.create(null) for dictionary-like data without inherited properties.

const stock = Object.create(null);
stock.apples = 12;
stock.apples += 1;

Maps

Map stores key-value pairs and permits keys of any type.

const roles = new Map();
roles.set("ada", "admin");
console.log(roles.get("ada"));

Sets

Set stores unique values. WeakSet stores objects weakly, so it does not prevent their garbage collection.

const tags = new Set(["javascript", "web"]);
tags.add("javascript"); // Duplicate ignored.

Typed arrays

Typed arrays store fixed-width numeric values in contiguous memory.

const pixels = new Uint8Array([255, 128, 0]);
pixels[1] = 64;

Queue

Avoid shift() for large queues because it reindexes the array. Track the front position instead.

class Queue {
  #items = [];
  #head = 0;

  enqueue(value) {
    this.#items.push(value);
  }

  dequeue() {
    return this.#head < this.#items.length
      ? this.#items[this.#head++]
      : undefined;
  }
}

Priority queue

JavaScript has no built-in priority queue. This small binary min-heap inserts values by priority.

class MinHeap {
  #items = [];

  push(value) {
    this.#items.push(value);
    let index = this.#items.length - 1;

    while (index > 0) {
      const parent = Math.floor((index - 1) / 2);
      if (this.#items[parent] <= value) break;
      this.#items[index] = this.#items[parent];
      index = parent;
    }

    this.#items[index] = value;
  }
}

const priorities = new MinHeap();
priorities.push(5);
priorities.push(1);

Custom linked nodes

Use an explicit node class when a linked representation is more appropriate than an array.

class Node {
  constructor(value, next = null) {
    this.value = value;
    this.next = next;
  }
}

const head = new Node(1, new Node(2));

See Data structures for shared concepts and examples across languages.