Decision Framework: Choosing the Right Collection
You'll learn a systematic checklist to pick the right data structure—Object, Array, Map, Set, or their weak variants—every time.
Choosing the right collection isn't about memorizing rules—it's about asking the right questions. With five collection types at your disposal, you need a decision framework that quickly narrows your options.
The Decision Checklist
Question 1: Do you need key-value pairs or just values?
- Values only → Consider
ArrayorSet - Key-value pairs → Consider
ObjectorMap
Question 2: Are keys definitely strings/symbols, or could they be anything?
- Strings/symbols only →
Objectworks fine - Any type (objects, functions, etc.) → Use
Map
Question 3: Do you need uniqueness guarantees?
- Yes, values must be unique →
Set - No, duplicates allowed →
Array
Question 4: Do you need frequent membership checks?
- Yes, lots of "does this exist?" checks →
SetorMap(O(1) lookup) - No, mostly just storing/iterating →
ArrayorObjectmay suffice
Question 5: Will keys/values be objects that should be garbage collected?
- Yes, don't want to prevent GC →
WeakMaporWeakSet - No, or need iteration → Stick with strong collections
Question 6: Do you need JSON serialization?
- Yes →
ObjectorArrayonly - No → Any collection works
Quick Decision Examples
- Shopping cart items? Array (ordered, duplicates possible)
- User preferences? Object (string keys, JSON-friendly)
- Tracking logged-in users by ID object? Map (any key type, frequent lookups)
- List of unique tags? Set (uniqueness enforced)
- Caching DOM element metadata? WeakMap (allows GC)
Key Takeaway: Ask about your data's keys, uniqueness needs, lookup patterns, and memory requirements—the answers will point you to the ideal collection type.