13
13
<a href =" https://npmcharts.com/compare/weakref?minimal=true " ><img alt =" Downloads " src =" https://img.shields.io/npm/dt/weakref.svg?style=flat-square " /></a >
14
14
</p >
15
15
16
- Weak Collection Library for Deno and Node.js.
16
+ This library provides three iterable weak data structures for JavaScript,
17
+ IterableWeakSet, IterableWeakMap, and InvertedWeakMap. These data structures are
18
+ designed to work with objects as keys or values, and are useful when you need to
19
+ store a collection of objects that may be garbage collected.
17
20
18
21
## Usage
19
22
20
23
### with Deno
21
24
22
25
``` ts
23
26
import {
27
+ InvertedWeakMap ,
24
28
IterableWeakMap ,
25
29
IterableWeakSet ,
26
30
} from " https://deno.land/x/weakref/mod.ts" ;
27
31
28
32
const set = new IterableWeakSet ();
29
33
const map = new IterableWeakMap ();
34
+ const invertedMap = new InvertedWeakMap ();
30
35
```
31
36
32
37
### with Node.js & Browser
@@ -45,6 +50,11 @@ import { IterableWeakMap, IterableWeakSet } from "weakref";
45
50
46
51
### IterableWeakSet
47
52
53
+ IterableWeakSet is a class that extends the WeakSet and Set classes in
54
+ JavaScript, allowing you to create a set of objects that can be iterated over.
55
+ Objects in the set are stored using weak references, which means that they can
56
+ be garbage collected if they are no longer referenced elsewhere in the program.
57
+
48
58
** Interface**
49
59
50
60
``` ts
@@ -59,23 +69,29 @@ class IterableWeakSet<T extends object> implements WeakSet<T>, Set<T> {
59
69
``` ts
60
70
const set = new IterableWeakSet ();
61
71
62
- for (let i = 0 ; i < 100 ; i ++ ) {
63
- set .add ({});
72
+ // create an object with a weak reference
73
+ {
74
+ const user
= { id:
1 , email:
" [email protected] " };
75
+ set .add (user );
64
76
}
77
+ // end of scope, user will be garbage collected
65
78
66
- for (const item of set ) {
67
- console .log (item ); // will print 100 items
79
+ // force garbage collection
80
+ if (global .gc ) {
81
+ global .gc ();
68
82
}
69
83
70
- // after garbage collection, {n} items will be collected
71
-
72
- for (const item of set ) {
73
- console .log (item ); // will print (100 - {n}) items
74
- }
84
+ // check the set size
85
+ console .log (set .size ); // output: 0
75
86
```
76
87
77
88
### IterableWeakMap
78
89
90
+ IterableWeakMap is a class that extends the WeakMap and Map classes in
91
+ JavaScript, allowing you to create a map of objects that can be iterated over.
92
+ Keys in the map are stored using weak references, which means that they can be
93
+ garbage collected if they are no longer referenced elsewhere in the program.
94
+
79
95
** Interface**
80
96
81
97
``` ts
@@ -90,23 +106,30 @@ class IterableWeakMap<K extends object, V> implements WeakMap<K, V>, Map<K, V> {
90
106
``` ts
91
107
const map = new IterableWeakMap ();
92
108
93
- for (let i = 0 ; i < 100 ; i ++ ) {
94
- map .set ({}, i );
109
+ // create an object with a weak reference
110
+ {
111
+ const user
= { id:
1 , email:
" [email protected] " };
112
+ const metadata = { created: new Date () };
113
+ map .set (user , metadata );
95
114
}
115
+ // end of scope, user will be garbage collected
96
116
97
- for (const [key, value] of map ) {
98
- console .log (key , value ); // will print 100 items
117
+ // force garbage collection
118
+ if (global .gc ) {
119
+ global .gc ();
99
120
}
100
121
101
- // after garbage collection, {n} items will be collected
102
-
103
- for (const [key, value] of map ) {
104
- console .log (key , value ); // will print (100 - {n}) items
105
- }
122
+ // check the map size
123
+ console .log (map .size ); // output: 0
106
124
```
107
125
108
126
### InvertedWeakMap
109
127
128
+ InvertedWeakMap is a class that allows you to create a map of non-object keys
129
+ with weak references to object values. This is useful when you have a collection
130
+ of non-object keys that you want to use to look up objects, and those objects
131
+ may be garbage collected.
132
+
110
133
** Interface**
111
134
112
135
``` ts
@@ -121,17 +144,18 @@ class InvertedWeakMap<K, V extends object> implements Map<K, V> {
121
144
``` ts
122
145
const map = new InvertedWeakMap ();
123
146
124
- for (let i = 0 ; i < 100 ; i ++ ) {
125
- map .set (i , {});
147
+ // create an object with a weak reference
148
+ {
149
+ const user
= { id:
1 , email:
" [email protected] " };
150
+ map .set (user .id , user );
126
151
}
152
+ // end of scope, user will be garbage collected
127
153
128
- for (const [key, value] of map ) {
129
- console .log (key , value ); // will print 100 items
154
+ // force garbage collection
155
+ if (global .gc ) {
156
+ global .gc ();
130
157
}
131
158
132
- // after garbage collection, {n} items will be collected
133
-
134
- for (const [key, value] of map ) {
135
- console .log (key , value ); // will print (100 - {n}) items
136
- }
159
+ // check the map size
160
+ console .log (map .size ); // output: 0
137
161
```
0 commit comments