Detailed explanation
Map, Set, plain objects, and arrays solve different data-modelling problems in JavaScript. A Map represents key–value associations, a Set represents unique values, a plain object usually represents a record with named properties, and an array represents an indexed sequence that allows duplicates. Choose based on the operations and meaning of your data, not just syntax. For example, use an object for a user record, an array for ordered tasks, a Map for a lookup table, and a Set for selected IDs.
Map accepts keys of any type, including objects, functions, and primitives, without converting them to strings. Object property keys, by contrast, are strings or symbols; other values are coerced into property keys. Two different object keys can therefore collide as properties, commonly becoming '[object Object]'. Map compares object keys by identity, so m.set({id: 1}, 'value') cannot be retrieved using m.get({id: 1}); you must retain the original reference. Maps iterate in insertion order and expose their entry count through size. Object property enumeration has defined ordering rules too, but integer-index keys precede other string keys, so it is not simply insertion order.
Set stores each distinct value once, making [...new Set(arr)] a convenient way to deduplicate an array while preserving first-occurrence order. Map keys and Set values use SameValueZero equality: NaN equals NaN, and +0 and -0 are treated as equal. Objects are still compared by reference, not their contents. Array includes() and indexOf() generally require a linear scan, whereas Map and Set implementations must provide average access times sublinear in collection size. Hash-based implementations commonly offer average constant-time lookup, insertion, and deletion, but JavaScript does not guarantee O(1) for every operation.
Practical differences also matter in interviews. Map and Set are directly iterable and provide explicit APIs such as has(), delete(), and clear(). Objects integrate naturally with JSON, while Maps and Sets need conversion or custom serialization. Arrays remain preferable for positional access, slicing, and sequence transformations. Interviewers look for appropriate choices, accurate equality semantics, and qualified performance claims rather than a blanket statement that Maps and Sets are always faster.
Key points to remember
- Map supports any key type; object property keys are strings or symbols.
- Set enforces uniqueness by value for primitives and by identity for objects.
- Map and Set preserve insertion order and expose size; arrays expose length.
- Map and Set access must be sublinear on average; guaranteed O(1) for all operations is an overstatement.
- Use objects for records, arrays for indexed sequences, Maps for keyed associations, and Sets for membership.
Common mistakes candidates make
- Claiming that object keys are only strings or that objects have no defined property ordering.
- Expecting separately created objects with identical properties to match as Map keys or collapse in a Set.
- Saying arrays always require O(n) access: membership searches are generally linear, but indexed access is typically constant time.
Likely follow-up questions
Does Set remove duplicate objects based on their properties?
No: new Set([{id: 1}, {id: 1}]) contains two entries because the objects have different identities. To deduplicate by id, explicitly track IDs with a Set or build a Map keyed by id.
Can an object safely act as a dictionary?
Yes, especially when its keys are strings or symbols. Object.create(null) avoids inherited properties, and Object.hasOwn() checks ownership safely; Map is often clearer for frequently changing key–value collections.
How do you serialize a Map or Set to JSON?
JSON.stringify() does not serialize their entries automatically; ordinary Maps and Sets become '{}'. Convert a Map to an entry array with [...map] and a Set to [...set], then reconstruct them with new Map(entries) or new Set(values); object-key identity will not survive the JSON round trip.
Interview tip: Start with the intended data model, demonstrate identity-based equality with an object key, and qualify performance claims.
