Why This Data Structure?

Frequent operations, the lookup you need, order, duplicates, size, churn — a recommendation with what every operation costs in it, why it won, and the answer that would make it lose.

I need XWhat is X?What must it remember?What can happen to it?What must always hold?ExamplesRepresent the stateWhich structure?Each operationPseudocodeImplement oneTest with examplesEdge casesIntegrate

"Use a map, it's O(1)" is the slogan. The falsifiable version: a map wins when the lookup by key is the frequent operation and the collection is large enough for a scan to be felt. A cart of four lines fails both halves, which is why the reference cart is an array. Answer the six questions and watch where the answer flips.

Start from a preset, or answer from scratch

Six questions

0 / 6 answered — the recommendation updates as you go.

Which operation happens most often?

The structure is chosen for the operation you do a thousand times, not the one you do once. This answer counts double.

How do you need to find a single element?

A key lookup is O(1) in a map and a scan in an array; a positional lookup is the reverse. The lookup you need decides which one pays.

Does order matter, and which order?

Insertion order is free in an array, a JavaScript Map and a Set. Sorted order is not free anywhere: something has to pay for it on every insert or every read.

What does adding the same thing twice mean?

Whether "the same product twice" is two rows, one row, or one row with quantity 2 is a rule of the concept, and each rule has a structure that enforces it for free.

How many elements can there be?

Complexity only bites at scale. A scan over ten elements is faster than a hash lookup; a scan over a million is the outage. This answer is what makes the recommendation flip.

How often does it change?

A structure that is sorted once and read many times can afford an O(n) insert. One that changes on every request cannot.

Recommendation

Incomplete — the model names the unanswered question that matters most.

primaryArrayarray →
because
  • The simplest structure that keeps insertion order; every language has it and every reader understands it.
  • Appending and walking through are the cheapest operations, and at tens of elements the O(n) lookup is faster than a hash.
  • No answer yet distinguishes the candidates; the array wins by being the simplest.

flips when the lookup by key becomes frequent and the array grows past thousands — every find is now a scan the user waits for.

warnings
Unanswered: frequent, lookup, order, duplicates, size, mutation. Answering frequent ("Which operation happens most often?") would change the recommendation most.
alternativeMaphash-map →

Map scores 0 against 0 — a tie broken toward the simpler structure: nothing you answered favours it. It wins when the lookup by key becomes frequent and the array grows past thousands — every find is now a scan the user waits for.

alternativeSethash-set →

Set scores 0 against 0 — a tie broken toward the simpler structure: nothing you answered favours it. It wins when the lookup by key becomes frequent and the array grows past thousands — every find is now a scan the user waits for.

What each operation costs — primary and alternatives
operationArrayMapSet
appendO(1) amortisedO(1)O(1)
lookup by keyO(n) scanO(1) averageO(1) average membership
lookup by positionO(1)O(n) walkO(n) walk
lookup by predicateO(n) scanO(n) scanO(n) scan
removeO(n) shiftO(1) by keyO(1)
iterate in orderO(n), insertion orderO(n), insertion orderO(n), insertion order
min / maxO(n) scanO(n) scanO(n) scan
range queryO(n) scanO(n) scanO(n) scan

8 canonical operations; the scorer reads the same table.

What if?

For each question, every other answer — and whether the primary would change.

Which operation happens most often? — now: unanswered
How do you need to find a single element? — now: unanswered
Does order matter, and which order? — now: unanswered
What does adding the same thing twice mean? — now: unanswered
How many elements can there be? — now: unanswered
How often does it change? — now: unanswered

Array cart against map cart

impl §59 — the same five operations, two representations; read off the cart concept's own records.

axisArray of CartItemMap from product id to CartItem
SimplicityThe simplest thing that could work; every language has one."One entry per product" becomes structural: the key cannot repeat.
LookupO(n) scanO(1) average
OrderingO(n), order preservedO(n); insertion order only where the language guarantees it
SerialisationSerialises to JSON directly.Serialising to JSON needs a conversion step in most languages.
Memoryone small record per entry, contiguous — nothing beside the datathe entries plus the hash table that indexes them — more per entry, and it only pays back when the lookup is felt
array — recommended when
A cart, which holds a handful of items and is rendered in order. This is the V1 choice.
map — recommended when
Large carts, or when the key uniqueness is the point — a wishlist with thousands of entries, or a server holding many carts keyed by owner.

the reference chose array A cart holds a handful of items and is shown in the order they were added; the O(n) scan is invisible at that size and the array serialises for free. The map is the right answer to a different question — see Why This Data Structure?

SIMPLIFIED

The scorer is a sum of small fit tables and a size penalty read off each structure's own complexity row — written down in `src/thinking/sim/structure.ts` so you can check it by hand. It ranks nine in-memory structures and rows; it does not know about tries, skip lists, LSM trees, or the cache line. Beyond memory it stops ranking and says "rows", because that is a different question.