JavaScript Map

You should use JavaScript Map when an object consists of ==key: value pairs and you frequently add and delete entries== (action performed).

Object to Map and vice versa

const eventMap = new Map();
 
// Object from Set
const obj = Object.fromEntries(eventsMap);
 
// Set from Object
const newMap = new Map(Oject.entries(obj));

Advantages over JavaScript Objects

  • Objects return true even if they’re empty, which is wat behaviour. Maps are always false when empty.
  • Maps will preserve order of keys