You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/08-weakmap-weakset/article.md
+13-12
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,7 @@ Let's see what it means on examples.
60
60
61
61
## WeakMap
62
62
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:
64
64
65
65
```js run
66
66
let weakMap =newWeakMap();
@@ -101,9 +101,9 @@ Compare it with the regular `Map` example above. Now if `john` only exists as th
101
101
102
102
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*.
103
103
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.
105
105
106
-
Now where do we need such data structure?
106
+
Now, where do we need such a data structure?
107
107
108
108
## Use case: additional data
109
109
@@ -147,7 +147,7 @@ countUser(john); // count his visits
147
147
john =null;
148
148
```
149
149
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`.
151
151
152
152
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.
153
153
@@ -164,13 +164,13 @@ function countUser(user) {
164
164
}
165
165
```
166
166
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`.
168
168
169
169
## Use case: caching
170
170
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.
172
172
173
-
We can use `Map`to store results, like this:
173
+
To achieve that, we can use `Map`(not optimal scenario):
174
174
175
175
```js run
176
176
// 📁 cache.js
@@ -207,7 +207,7 @@ alert(cache.size); // 1 (Ouch! The object is still in cache, taking memory!)
207
207
208
208
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.
209
209
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.
211
211
212
212
```js run
213
213
// 📁 cache.js
@@ -248,7 +248,7 @@ obj = null;
248
248
- An object exists in the set while it is reachable from somewhere else.
249
249
- Like `Set`, it supports `add`, `has` and `delete`, but not `size`, `keys()` and no iterations.
250
250
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.
252
252
253
253
For instance, we can add users to `WeakSet` to keep track of those who visited our site:
254
254
@@ -276,14 +276,15 @@ john = null;
276
276
// visitedSet will be cleaned automatically
277
277
```
278
278
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.
280
280
281
281
## Summary
282
282
283
283
`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.
284
284
285
285
`WeakSet` is `Set`-like collection that stores only objects and removes them once they become inaccessible by other means.
286
286
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` ...
288
289
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