Course contentsShow
← JavaScript
Lesson 1072 of 2,72324. Maps and SetsFree lesson

Decision Framework: Choosing the Right Collection

Apply a systematic checklist to select Object, Array, Map, Set, or their weak variants.

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 Array or Set
  • Key-value pairs → Consider Object or Map

Question 2: Are keys definitely strings/symbols, or could they be anything?

  • Strings/symbols only → Object works 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 → Set or Map (O(1) lookup)
  • No, mostly just storing/iterating → Array or Object may suffice

Question 5: Will keys/values be objects that should be garbage collected?

  • Yes, don't want to prevent GC → WeakMap or WeakSet
  • No, or need iteration → Stick with strong collections

Question 6: Do you need JSON serialization?

  • Yes → Object or Array only
  • 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.