[PDF] javascript - maps & weakmaps

Understanding Strong References

Let’s start by taking a look at what a normal, or strong, reference is in JavaScript. In its simplest definition, a strong reference is a reference that keeps an object in memory. Let’s take a look at this in practice to understand what we are talking about: By creating a variable as an object, we can place the object into an array and remove the r...

Understanding Weak References

Simply put a weak reference is a reference to an object that doesn’t prevent garbage collection if it is the only reference to the object in the memory. A normal reference (considered strong), would prevent the garbage collection of an object even if it is the only object referencing it; this isn’t the case for a weak reference. Let’s take this the...

What Is Garbage Collection?

While garbage collection is a detailed and complicated subject, it is important to understand when discussing references. Garage collection is an automated process controlled by the JavaScript engine. When a value is reachable, it is guaranteed to be stored in memory and not garbage collected, and there are two ways a value is considered reachable....

Sets vs. Weaksets

Per MDN, “Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set‘s collection.” Simply put, a Set is like an array that can only contain unique values but we can still iterate through it like an array using methods like for loops and .f...

Maps vs. WeakMap

According to MDN, “The Mapobject holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.” This means a Map is like an object where we can store key-value pairs and access the values contained within the Map through the key. Unlike a standard ...

View PDF Document


What is a WeakMap in JavaScript?

A WeakMap is a collection of key/value pairs whose keys must be objects or non-registered symbols, with values of any arbitrary JavaScript type, and which does not create strong references to its keys. That is, an object's presence as a key in a WeakMap does not prevent the object from being garbage collected.

How do you enumerate a weak map?

Any scheme that involves saving the ID of a promise inherently means you need a reference to it. WeakMaps mean that the keys are weak. There are no ways to enumerate a weak map or to get all its values. In a weak map, you can store the data based on a key and when the key gets garbage collected so do the values.

Is there a way to get all keys and values from WeakMap?

WeakMap does not support iteration and methods keys (), values (), entries (), so there’s no way to get all keys or values from it. That is obvious as if an object has lost all other references (like 'user' in the code above), then it is to be garbage-collected automatically.

What is a map in JavaScript?

Any value (both objects and primitive values) may be used as either a key or a value.” This means a Map is like an object where we can store key-value pairs and access the values contained within the Map through the key. Unlike a standard object in JavaScript, however, we must use the .get() method to access the values.

View PDF Document






















M1IF03 Conception dApplications Web

ECMAScript Harmony = ES 2015 = ES6 (06/2015) ? JavaScript 2.0. ? ES7 (06/2016). ? ES.Next… ES10 Collections (maps sets and weak maps). ? Promises.



Your Complete Guide to the New Features Introduced in JavaScript

Raju Gandhi is a programmer with over 20 years of experience in the software industry. He believes that the key to writing software users will cherish lies 



JavaScript ES6 (ES2015)

L'élément HTML <script> permet d'utiliser du code JavaScript avec une page HTML. L'objet WeakMap permet de créer des maps avec des clés qui sont.



You Dont Know JS: ES6 & Beyond

JS: ES6 & Beyond the cover image