This section explains how core data structures in JavaScript actually behave at runtime, their performance characteristics, and when to use each one. The goal is not to re‑implement data structures academically, but to understand trade‑offs, guarantees, and pitfalls in real JavaScript code.
The exercises are designed to test whether you can reason about correctness, complexity, and semantics, not just syntax.
Choosing the wrong data structure often leads to:
- Hidden performance problems
- Incorrect assumptions about ordering or uniqueness
- Accidental quadratic behavior
- Bugs caused by reference sharing
Senior‑level JavaScript requires understanding what the engine guarantees and what it does not.
Arrays are ordered, indexed collections optimized for:
- Fast indexed access
- Append/remove at the end
const arr = [1, 2, 3];
arr.push(4); // O(1) amortizedImportant properties:
- Arrays are objects
- Indices are string keys under the hood
lengthis mutable
push/pop: O(1) amortizedshift/unshift: O(n)- Random access: O(1)
Plain objects can act as key‑value maps:
const map = { a: 1 };But there are caveats:
- Keys are strings or symbols
- Prototype pollution risks
- No guaranteed insertion order for all operations historically
Use objects when:
- Keys are known and controlled
- You need JSON‑serializable structures
Map is a true hash map:
const m = new Map();
m.set({}, 1);Properties:
- Keys can be any value
- Maintains insertion order
- Predictable iteration
Performance:
get/set/has: O(1) average
Set stores unique values:
const s = new Set([1, 2, 2]);Properties:
- Uniqueness based on SameValueZero (
NaNallowed once) - Preserves insertion order
Weak collections:
- Keys must be objects
- Do not prevent garbage collection
- Not iterable
const wm = new WeakMap();Use cases:
- Private data
- Metadata tied to object lifetime
Common implementations using arrays:
// Stack
push / pop
// Queue (simple, but O(n) dequeue)
push / shiftFor performance‑critical queues, use linked list–style or two‑stack techniques.
JavaScript does not have a native linked list, but the concept matters:
- O(1) insert/remove with node reference
- O(n) traversal
Exercises may simulate this with objects.
Important equality rules:
Map/Setuse SameValueZero- Objects compare by identity, not structure
{} !== {}
NaN === NaN // false
// but
new Set([NaN, NaN]).size === 1Different structures iterate differently:
- Arrays: index order
- Objects: own enumerable string keys
- Map / Set: insertion order
Understanding iteration guarantees is critical for correctness.
| Need | Use |
|---|---|
| Ordered list | Array |
| Key → value (any key) | Map |
| Unique values | Set |
| Private metadata | WeakMap |
The exercises rely on understanding:
- Time complexity of operations
- Identity vs structural equality
- Reference semantics
- Iteration guarantees
- Garbage‑collection behavior (weak refs)
If performance or correctness surprises you, ask:
What guarantees does this data structure actually provide?
These exercises test whether you can:
- Pick appropriate data structures
- Reason about complexity
- Avoid identity and mutation bugs
If you can explain why a structure behaves the way it does, you understand JavaScript data structures.