concept Map in category javascript

appears as: Map, Map, A Map, Map
Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

This is an excerpt from Manning's book Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond.

Common built-in iterables are Strings and Arrays but so are Sets and Maps. Because all of the objects follow a common protocol, it means they all behave in similar ways. You can also use this protocol to create your own iterables or customize the behavior of default ones!

As I mentioned, not all iterables are new. JavaScript has always had strings and arrays. But the protocol that sets a contract for how strings, arrays, and other iterators behave is new. Strings and arrays have been updated to use this new protocol, and other objects like Sets and Maps are new objects that also make use of the protocol.

  • Know to create a Map
  • How to convert a plain object into a Map
  • How to add and access values on a Map
  • How to iterate the keys and values from a Map
  • How Maps are destructured
  • Understand when it makes sense to use a Map over an Object
  • Understand why it makes sense to use a WeakMap over a Map
  • Like Sets, Maps are a new type of object in JavaScript. Not to be confused with Array.prototype.map, which is a higher-order array method, Maps are another type of iterable that’s new to JavaScript in ES2015. A Map is a lot like a generic object in JavaScript with keys and values. But a Map can be iterated, whereas an object can’t. Plain objects are also limited to only being able to have string values as keys;[1] Maps, on the other hand, can have any datatype as a key, even another Map!

    As with Sets, there’s no literal version of a Map and they must be created using the new keyword like so:

    const myMap = new Map();

    Remember from the last lesson that you can instantiate a Set with a single array argument, to define the initial values for the set? You may be thinking that because a Map is a series of keys and values, you can instantiate a Map with initial values by passing in a single object parameter. This isn’t the case, though. But if you think about it, this makes sense. An array can contain any datatype that a set can, but an object’s keys are limited to strings, whereas a Map’s keys can be any datatype. So using an object as the initial parameter would limit what kind of keys you could use. Instead, to instantiate a Map with initial values, you use one large array containing smaller arrays of key/value pairs like so:

    Similar to what you learned about the WeakSet in lesson 21, a WeakMap is a non-iterable subset of the Map. But whereas the WeakSet has weak references to its values, the WeakMap has weak references to its keys. Additionally, a WeakMap can only have objects as its keys. The WeakMap doesn’t keep its keys from becoming eligible for garbage collection. Since the WeakMap isn’t iterable, there’s no way to get a list of all the keys of a WeakMap. Also there’s no way to check if a WeakMap has a specific key unless you already have a reference to that key. Once there are no more references to a key, it will no longer be accessible from the WeakMap and will become garbage-collected.

    You may want to use a WeakMap when you’re trying to avoid memory leaks and you don’t need to iterate all the keys/values in the Map, but only need to access the value once you already have a reference to the key. Say, for example, you want to store metadata about some objects. Maybe the objects are logged-in players of a game and they may sign off. Or maybe the objects are DOM nodes that may be removed from the DOM as the UI changes. If you store metadata based on these objects in a regular Map, you’re creating a memory leak because the Map’s reference to these objects will prevent them from being garbage-collected. A WeakMap wouldn’t prevent them from being garbage-collected and thus wouldn’t create a memory leak. If you need to use a Map, though, because you need some additional features like iteration, you can still use it; you just need to manage removing the objects from the Map when necessary.