New data types Map, Set & Symbol

New data types Map, Set & Symbol

Symbol

Features

Symbol is a new data type that came with the ES2015 standard. Symbol represents a unique token. A symbol is created using the function Symbol().

A symbol can also be created with a label, which is passed to it as a string. That label does not affect the value of the symbol, but is useful for debugging and can be displayed using the toString() method on the symbol.

Purpose: Object property key

Mostly “symbol” is used as the key of an object property, when we want to protect that property. When using “symbol” as the key of an object’s property, it is necessary to use square brackets to access the property (the so-called “bracket notation”). Such properties have the following characteristics:

  • Unique Property
  • The property cannot be enumerated through a “for…in” loop
  • The property is ignored by the methods: Object.keys(), Object.getOwnPropertyNames() and JSON.stringify()

It should be noted that the privacy of this property is not 100%, because there is still a way to access the property via the methods “.getOwnPropertySymbols()” and .ownKeys():

Map

Features

es2015

Map is a collection of key/value pairs, where the key can be any type of data, unlike an object where the key can be a bit or a string or a symbol. It has a whole set of built-in methods for working with collection elements:

  • .set() – defining a new pair
  • .delete() – delete a pair
  • .get() – get key value
  • .keys() – list of all keys
  • .values() – list of all values
  • .entries() – returns all key/value pairs in separate strings
  • .clear() – delete the contents of the object
Example

Difference between object and Map

Map is a new object subtype that is very similar to object but with a few important differences:

Object Map The key can only be String or Symbol. The map key can be any type, even “NaN” type. Assigning a value to a key with the = operator. Assigning a value to a key with built-in methods: .set() and .get(). Object properties are not iterable collections (so cannot use forEach() or for..of method to iterate through properties). Map is an iterable collection (so it can forEach or for of is used to iterate through the elements) . We cannot simply get the size of objects.

The

.size property simply returns the size. Keys must be unique otherwise we get an error. The keys do not have to be unique, because if two members with the same key are defined, the newer one “overrides” the older one Code must be written to delete properties. The map has a built-in method .clear() with which it simply deletes everything. If all keys are strings, and you just use get and set, Objects are more performant. If you focus on iterating, the map will be more performant. (TEST)

Iteration through Map

The standard loop for..of as well as the method forEach()

are available for iterating through pairs.

Converting object to “map”

We can convert the object to a new JavaScript type “map”. The map constructor new Map() accepts as arguments only iterable collections, therefore we need to create a series of [key, value] pairs from the properties of the object using the method “Object.entries(obj)”:

Example

WeakMap

WeakMap is also a collection of key/value pairs, but it is the “key” that makes the main difference between Map and WeakMap. The key in case of WeakMap cannot be any data type other than “object”. WeakMap has the same API as Map: set(), delete( get(), keys(), values(), entries(), clear().

Difference between Map and WeakMap

Map WeakMap The key can be any data type. The key can be “only of type “Object”” (but not “null”). Map has a size method .size WeakMap has a size method .length Iteration with method .forEach(). The key of WeakMap type is not “enumerable” so it does not have a method for listing the keys ie. cannot use the .foreach() method for iteration. Nothing is “auto destroy”. If an object that is a key in a WeakMap pair is deleted, the value assigned to that key will also be deleted and will not take up memory.
Example

This last difference is the most important, and I will explain it in detail through an example. We will consider the case when both Map and WeakMap have object

‘s key properties

Map and garbage collector

When the compiler reaches the line where the IIFE is and executes it, we no longer need the variable “a”, but the garbage collector will not clear it from memory.

WeakMap and garbage collector

When the compiler reaches the line where the IIFE is and executes it, we no longer need the variable “b”, since the reference to that object is “weak”, the garbage collector will delete the given variable and free the memory.

The conclusion from the previous example is that WeakMap has a better memory utilization than the Map data type where a memory leak can occur.

Set

Features

The set data type is very similar to arrays, except that it does not allow duplicate values. The methods available through the “Set.prototype” object are:

  • .add() – adding a new member
  • .delete(value) – delete the corresponding member
  • .has(value) – returns a boolean value
  • .values() – list of all values
  • .entries() – returns all key/value pairs in separate strings
  • .clear() – delete the contents of the object
  • .forEach(callback) – Iterate through the object

A properties:

  • .size – total number of pairs
  • .constructor – returns the function that created the instance
Example

Difference between arrays and Set collection

Array Set The value of elements can be duplicated Contains only unique values Focused on index and need to write code to find specific element and delete Focused on the value of the element. Vresity can be found and deleted very easily. Method .push() to add elements Method .add() to add elements Checking the existence of an element with the method .includes() (ES7) Checking the existence of an element with the .has() method The .length property is used to determine the number of elements The .size property is used to determine the number of elements The arr.splice(0,arr.length) method is used to empty the collection The mySet.clear() method is used to empty the collection

Iteration through Set

Iteration is possible using the for..of:

loop

or the available forEach():

method

Converting to strings

Using the “from()” method, we can create an array from the set object:

The reverse is also true:

and even this:

WeakSet

WeakSet is a specific subtype of the Set data type because it can only contain a collection of objects.

Difference between Set and WeakSet

Set WeakSet They can contain any type of data They can only store objects The number of collection members is obtained using the .size property The number of collection members is obtained using the .length property Iterable collection, can use .forEach() or for..of Not an iterable collection and cannot use .forEach() or for..of Not auto destroyed If the object no longer has a reference it will be picked up by the garbage collector
Example

This last difference is the most important, and I will explain it in detail through an example. We will consider the case when both Set and WeakSet have object

‘s key properties

Set and garbage collector

When the compiler reaches the line where the IIFE is and executes it, we no longer need the variable “a”, but the garbage collector will not clear it from memory.

WeakSet and garbage collector

When the compiler reaches the line where the IIFE is and executes it, we no longer need the variable “b”, since the reference to that object is “weak”, the garbage collector will delete the given variable and free the memory.

The conclusion from the previous example is that the WeakSet has a better memory utilization than the Set data type where a memory leak can occur.