ES6 — Map vs Object — What and when?

Maya Shavin
Frontend Weekly
Published in
10 min readFeb 1, 2018

--

Object vs Map in a happy way

You may wonder — why Map vs Object but not Map vs Array, or Object vs Set? Well, you can also compare between any of the two, but Map and Object, unlike others, have very similar use-cases which require us to understand more deeply about them to decide what is better for when. And that’s what this is about.

Let’s start, shall we?

The concepts

What is Map?

Map sounds very simple, doesn’t it? We see or hear about it almost everyday, let’s say World map, Street map, etc…. So what is exactly Map?

Map is a data collection type (in a more fancy way — abstract data structure type), in which, data is stored in a form of pairs, which contains a unique key and value mapped to that key. And because of the uniqueness of each stored key, there is no duplicate pair stored.

You may recognize by now one common thing about all examples mentioned above — they are used for looking up something (can be a country — for World map, a street name — Street map, etc…).

That’s right, Map is mainly used for fast searching and looking up data.

Example: {(1, “smile”), (2, “cry”), (42, “happy”)}

in which each pair is in the format — (key, value).

--

--