Skip to content

Complexity Cheat Sheet

Quick reference for time and space complexity of every pattern's key operations. Use this to compare trade-offs before choosing a pattern.

How to Read This

  • n = number of elements / items
  • k = key length (for string-keyed structures)
  • m = number of hash functions (Bloom filter)
  • L = number of levels (skip list, LSM tree)
  • Amortized costs marked with (amort.)

Data Structures

PatternInsertLookupDeleteSpaceNotes
BitmaskO(1)O(1)O(1)O(1)Fixed-size; limited to word width (32/64 flags)
Ring BufferO(1)O(1)O(1)O(n)Fixed capacity; overwrites oldest on full
Tagged UnionO(1)O(max variant)Dispatch by tag; no dynamic allocation
Min HeapO(log n)O(1) peekO(log n)O(n)O(1) min access; used for priority queues
TrieO(k)O(k)O(k)O(n × k)Independent of total entries; prefix queries O(k + results)
Bloom FilterO(m)O(m)O(n)Probabilistic; false positives possible, no false negatives
LRU CacheO(1)O(1)O(1)O(n)Hash map + doubly linked list
Skip ListO(log n) avgO(log n) avgO(log n) avgO(n)Probabilistic balancing; range queries supported
B+ TreeO(log n)O(log n)O(log n)O(n)Disk-optimized; high fan-out minimizes I/O
Merkle TreeO(log n)O(log n)O(log n)O(n)Verification O(log n) proofs
Merge IteratorO(log k) nextO(k)k = number of streams; heap-based merge

Concurrency

PatternKey OperationCostSpaceNotes
Semaphoreacquire / releaseO(1)O(1)Counter-based; may block on contention
Double BufferingswapO(1)O(2n)Pointer swap; no copy
Event Loopenqueue / dequeueO(1)O(queue)Single-threaded; I/O multiplexed
Backpressuresignal / checkO(1)O(1)Flow control; often piggybacked on existing channel
Copy-on-WritereadO(1)O(n) per snapshotWrite triggers O(n) clone; reads never block
Cooperative SchedulingyieldO(1)O(tasks)Requires voluntary yield points
MVCCread / writeO(1) + GCO(n × versions)Reads never block; GC cost amortized
Work Stealingpush / stealO(1) amort.O(tasks)Lock-free deque; cache-line aware
Actor Modelsend messageO(1)O(actors × mailbox)Isolated state; no shared memory
Logical Clocktick / mergeO(1) Lamport, O(n) VectorO(1) / O(n)Vector clock grows with node count

System

PatternKey OperationCostSpaceNotes
State MachinetransitionO(1)O(states)Constant-time dispatch; explicit states
Circuit Breakercall / checkO(1)O(1)Counter + timer; 3 states
Rate Limiterallow?O(1)O(1) per limiterToken bucket or sliding window
Retry with BackoffretryO(retries) totalO(1)Exponential delay + jitter
Batch ProcessingflushO(batch)O(batch)Amortizes per-item overhead
Middleware ChainexecuteO(middlewares)O(middlewares)Linear pipeline; each handler O(1)
Registryregister / lookupO(1) hashO(n)String-keyed service locator
Dirty Flagcheck / markO(1)O(1)Boolean guard; skip unchanged
Dependency Graphtopo sortO(V + E)O(V + E)DAG; detects cycles
Consistent Hashinglookup nodeO(log n)O(n × vnodes)Binary search on ring; minimal reshuffling
Write-Ahead LogappendO(1) amort.O(log size)Sequential writes; fsync for durability
CheckpointingsnapshotO(state size)O(state size)Periodic; truncates WAL
LSM Treewrite / readO(1) write, O(L) readO(n)Write-optimized; compaction in background

Memory

PatternAllocateFreeLookupSpaceNotes
Object PoolO(1)O(1)O(pool size)Pre-allocated; avoids GC pressure
FlyweightO(1)O(unique)Share identical instances
Arena AllocatorO(1) bumpO(1) bulkO(arena size)Bump pointer; free all at once
Free ListO(1)O(1)O(n)Linked list of freed slots
Copy-on-WriteO(1) shareO(n) on writeO(1)O(n) per snapshotDeferred copy
Reference CountingO(1) cloneO(1) dropO(1) per refDeterministic; no cycles
TombstoneO(1) markO(1)O(n + deleted)Soft delete; compacted later
InterningO(k) first, O(1) afterO(1)O(unique × k)Hash-based dedup; compare by pointer

Behavioral

PatternKey OperationCostSpaceNotes
ObservernotifyO(subscribers)O(subscribers)Fan-out; order may vary
IteratornextO(1) per stepO(1)Lazy evaluation; pull-based
Diff / PatchdiffO(n × m) MyersO(n + m)Minimal edit distance
VtabledispatchO(1)O(methods)Pointer indirection; static resolution
VisitorvisitO(nodes)O(tree depth)Double dispatch; traversal + operation

Key Trade-offs Summary

If you need...ChooseTrade-off
O(1) lookup + O(1) evictionLRU CacheExtra memory for doubly linked list
O(1) writes at scaleLSM TreeRead amplification (multiple levels)
O(1) membership testBloom FilterFalse positives (no false negatives)
O(1) allocationArena or Free ListNo individual free (arena) or fragmentation (free list)
O(log n) sorted accessSkip List or B+ TreeSkip list simpler, B+ tree disk-optimized
Zero-copy readsCopy-on-Write or MVCCWrite amplification on mutation
Minimal rehash on scaleConsistent HashingVirtual nodes add memory overhead

Released under the MIT License.