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
+7-8
Original file line number
Diff line number
Diff line change
@@ -110,9 +110,9 @@ Now where do we need such data structure?
110
110
111
111
The main area of application for `WeakMap` is an *additional data storage*.
112
112
113
-
There are objects managed elsewhere in the code, maybe they come from a third-party code, and in our code we need to keep additional information that is only relevant while the object is in memory.
113
+
If we're working with an object that "belongs" to another code, maybe even a third-party library, and would like to store some data associated with it, that should only exist while the object is alive - then `WeakMap` is the right choice!
114
114
115
-
And when the object is garbage collected, that data should automatically disappear as well.
115
+
We put the data to a `WeakMap`, using the object as the key, and when the object is garbage collected, that data will automatically disappear as well.
For instance, we have code that keeps a visit count for each user. The information is stored in a map: a user object is the key and the visit count is the value. When a user leaves (its object gets garbage collected), we don't want to store their visit count anymore.
124
+
For instance, we have code that keeps a visit count for users. The information is stored in a map: a user object is the key and the visit count is the value. When a user leaves (its object gets garbage collected), we don't want to store their visit count anymore.
125
125
126
126
Here's an example of a counting function with `Map`:
127
127
@@ -136,7 +136,7 @@ function countUser(user) {
136
136
}
137
137
```
138
138
139
-
Let's imagine another part of the code using it:
139
+
And here's another part of the code, maybe another file using it:
140
140
141
141
```js
142
142
// 📁 main.js
@@ -151,7 +151,7 @@ john = null;
151
151
152
152
Now, we have a problem: `john` object should be garbage collected, but remains is memory, as it's a key in `visitsCountMap`.
153
153
154
-
We need to clean up `visitsCountMap` when we remove users, otherwise it will grow in memory indefinitely. Such cleaning can become a tedious task in complex architectures.
154
+
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.
155
155
156
156
We can avoid it by switching to `WeakMap` instead:
@@ -281,10 +280,10 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati
281
280
282
281
## Summary
283
282
284
-
`WeakMap` is `Map`-like collection that allows only objects as keys and removes them once they become inaccessible by other means.
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.
285
284
286
285
`WeakSet` is `Set`-like collection that only stores objects and removes them once they become inaccessible by other means.
287
286
288
-
Both of them do not support methods and properties that refer to all keys or their count. Only individial get/has/set/remove operations with a given key are allowed.
287
+
Both of them do not support methods and properties that refer to all keys or their count. Only individial operations are allowed.
289
288
290
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.
0 commit comments