Skip to content

Commit 9065fd3

Browse files
authored
Merge pull request #2302 from patrikbego/patch-3
Rephrased for clarity.
2 parents 361c702 + 79710c4 commit 9065fd3

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Diff for: 1-js/05-data-types/08-weakmap-weakset/article.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Let's see what it means on examples.
6060

6161
## WeakMap
6262

63-
The first difference from `Map` is that `WeakMap` keys must be objects, not primitive values:
63+
The first difference between `Map` and `WeakMap` is that keys must be objects, not primitive values:
6464

6565
```js run
6666
let weakMap = new WeakMap();
@@ -101,9 +101,9 @@ Compare it with the regular `Map` example above. Now if `john` only exists as th
101101

102102
Why such a limitation? That's for technical reasons. If an object has lost all other references (like `john` in the code above), then it is to be garbage-collected automatically. But technically it's not exactly specified *when the cleanup happens*.
103103

104-
The JavaScript engine decides that. It may choose to perform the memory cleanup immediately or to wait and do the cleaning later when more deletions happen. So, technically the current element count of a `WeakMap` is not known. The engine may have cleaned it up or not, or did it partially. For that reason, methods that access all keys/values are not supported.
104+
The JavaScript engine decides that. It may choose to perform the memory cleanup immediately or to wait and do the cleaning later when more deletions happen. So, technically, the current element count of a `WeakMap` is not known. The engine may have cleaned it up or not, or did it partially. For that reason, methods that access all keys/values are not supported.
105105

106-
Now where do we need such data structure?
106+
Now, where do we need such a data structure?
107107

108108
## Use case: additional data
109109

@@ -147,7 +147,7 @@ countUser(john); // count his visits
147147
john = null;
148148
```
149149

150-
Now `john` object should be garbage collected, but remains in memory, as it's a key in `visitsCountMap`.
150+
Now, `john` object should be garbage collected, but remains in memory, as it's a key in `visitsCountMap`.
151151

152152
We need to clean `visitsCountMap` when we remove users, otherwise it will grow in memory indefinitely. Such cleaning can become a tedious task in complex architectures.
153153

@@ -164,13 +164,13 @@ function countUser(user) {
164164
}
165165
```
166166

167-
Now we don't have to clean `visitsCountMap`. After `john` object becomes unreachable by all means except as a key of `WeakMap`, it gets removed from memory, along with the information by that key from `WeakMap`.
167+
Now we don't have to clean `visitsCountMap`. After `john` object becomes unreachable, by all means except as a key of `WeakMap`, it gets removed from memory, along with the information by that key from `WeakMap`.
168168

169169
## Use case: caching
170170

171-
Another common example is caching: when a function result should be remembered ("cached"), so that future calls on the same object reuse it.
171+
Another common example is caching. We can store ("cache") results from a function, so that future calls on the same object can reuse it.
172172

173-
We can use `Map` to store results, like this:
173+
To achieve that, we can use `Map` (not optimal scenario):
174174

175175
```js run
176176
// 📁 cache.js
@@ -207,7 +207,7 @@ alert(cache.size); // 1 (Ouch! The object is still in cache, taking memory!)
207207

208208
For multiple calls of `process(obj)` with the same object, it only calculates the result the first time, and then just takes it from `cache`. The downside is that we need to clean `cache` when the object is not needed any more.
209209

210-
If we replace `Map` with `WeakMap`, then this problem disappears: the cached result will be removed from memory automatically after the object gets garbage collected.
210+
If we replace `Map` with `WeakMap`, then this problem disappears. The cached result will be removed from memory automatically after the object gets garbage collected .
211211

212212
```js run
213213
// 📁 cache.js
@@ -248,7 +248,7 @@ obj = null;
248248
- An object exists in the set while it is reachable from somewhere else.
249249
- Like `Set`, it supports `add`, `has` and `delete`, but not `size`, `keys()` and no iterations.
250250

251-
Being "weak", it also serves as an additional storage. But not for an arbitrary data, but rather for "yes/no" facts. A membership in `WeakSet` may mean something about the object.
251+
Being "weak", it also serves as additional storage. But not for arbitrary data, rather for "yes/no" facts. A membership in `WeakSet` may mean something about the object.
252252

253253
For instance, we can add users to `WeakSet` to keep track of those who visited our site:
254254

@@ -276,14 +276,15 @@ john = null;
276276
// visitedSet will be cleaned automatically
277277
```
278278

279-
The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterations, and inability to get all current content. That may appear inconvenient, but does not prevent `WeakMap/WeakSet` from doing their main job -- be an "additional" storage of data for objects which are stored/managed at another place.
279+
The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterations, and the inability to get all current content. That may appear inconvenient, but does not prevent `WeakMap/WeakSet` from doing their main job -- be an "additional" storage of data for objects which are stored/managed at another place.
280280

281281
## Summary
282282

283283
`WeakMap` is `Map`-like collection that allows only objects as keys and removes them together with associated value once they become inaccessible by other means.
284284

285285
`WeakSet` is `Set`-like collection that stores only objects and removes them once they become inaccessible by other means.
286286

287-
Both of them do not support methods and properties that refer to all keys or their count. Only individual operations are allowed.
287+
It's main advantages are that they have weak reference to objects, so they can easily be removed by garbage colector.
288+
That comes at the cost of not having support for `clear`, `size`, `keys`, `values` ...
288289

289-
`WeakMap` and `WeakSet` are used as "secondary" data structures in addition to the "main" object storage. Once the object is removed from the main storage, if it is only found as the key of `WeakMap` or in a `WeakSet`, it will be cleaned up automatically.
290+
`WeakMap` and `WeakSet` are used as "secondary" data structures in addition to the "primary" object storage. Once the object is removed from the primary storage, if it is only found as the key of `WeakMap` or in a `WeakSet`, it will be cleaned up automatically.

0 commit comments

Comments
 (0)