Bit #33
When to use Object
VS Map
in Javascript:
Use Object
for records where you have a fixed and finite number of properties/fields known at author time, such as a config object or anything that is for one-time use in general.
Use Map
for dictionaries or hash maps where you have a variable number of entries, with frequent updates, whose keys might not be known at author time, such as an event emitter.
According to benchmarks, unless the keys are strings of small integers, Map
is indeed more performant than Object
on insertion, deletion and iteration speed, and it consumes less memory than an Object
of the same size.