Skip to content

Commit 7c340a1

Browse files
committed
minor
1 parent 2b18776 commit 7c340a1

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ Now where do we need such data structure?
110110

111111
The main area of application for `WeakMap` is an *additional data storage*.
112112

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!
114114

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.
116116

117117
```js
118118
weakMap.set(john, "secret documents");
@@ -121,7 +121,7 @@ weakMap.set(john, "secret documents");
121121

122122
Let's look at an example.
123123

124-
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.
125125

126126
Here's an example of a counting function with `Map`:
127127

@@ -136,7 +136,7 @@ function countUser(user) {
136136
}
137137
```
138138

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:
140140

141141
```js
142142
// 📁 main.js
@@ -151,7 +151,7 @@ john = null;
151151

152152
Now, we have a problem: `john` object should be garbage collected, but remains is memory, as it's a key in `visitsCountMap`.
153153

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.
155155

156156
We can avoid it by switching to `WeakMap` instead:
157157

@@ -271,7 +271,6 @@ alert(visitedSet.has(john)); // true
271271
// check if Mary visited?
272272
alert(visitedSet.has(mary)); // false
273273

274-
// John object is not needed any more
275274
john = null;
276275

277276
// visitedSet will be cleaned automatically
@@ -281,10 +280,10 @@ The most notable limitation of `WeakMap` and `WeakSet` is the absence of iterati
281280

282281
## Summary
283282

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.
285284

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

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.
289288

290289
`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

Comments
 (0)