Skip to content

Latest commit

 

History

History
47 lines (30 loc) · 2.69 KB

javascript-maps-and-sets.md

File metadata and controls

47 lines (30 loc) · 2.69 KB

JavaScript Maps and Sets

Week 3 Keywords and Questions

(see resource on Map and Set with examples)

  • What is a JS Map?
  • When should I use a Map?
  • What is a JS Set?
  • When should I use a Set?

Prerequisites

Motivation

ES6 brought with it some powerful upgrades, and the inclusion of Maps and Sets are two of them.

A Map is a collection of elements where each element is stored as a Key, value pair.

  • Map object can hold both objects and primitive values as either key or value.
  • You can use objects of all kinds as map keys.
  • The system will not automatically convert the keys to strings as it does for object literals.
  • Unlike with objects, map keys can be of any type, even objects or functions.
  • With maps we can iterate in the order in which the values were added, unlike objects where there’s no guarantee about the order.

A Set is a special type collection – “set of values” (without keys), where each value may occur only once.

  • The values in a set can be either simple primitives like strings or integers, or more complex object types like object literals or arrays

Materials

When to use map? And when to use object?

  • Object is the great choice for scenarios when we only need simple structure to store data and knew that all the keys are either strings or integers, because creating plain object and accessing object’s property with a specific key is much faster than creating a map.

  • Map preserves the order of its keys unlike object, and map was built with iteration in mind, so in case iteration or elements order are highly significant, consider map — it will ensure stable iteration performance in all browsers.

  • Map tends to perform better in storing large set of data, especially when keys are unknown until run time, and when all keys are the same type and all values are the same type.