1313 <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 >
1414</p >
1515
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.
1720
1821## Usage
1922
2023### with Deno
2124
2225``` ts
2326import {
27+ InvertedWeakMap ,
2428 IterableWeakMap ,
2529 IterableWeakSet ,
2630} from " https://deno.land/x/weakref/mod.ts" ;
2731
2832const set = new IterableWeakSet ();
2933const map = new IterableWeakMap ();
34+ const invertedMap = new InvertedWeakMap ();
3035```
3136
3237### with Node.js & Browser
@@ -45,6 +50,11 @@ import { IterableWeakMap, IterableWeakSet } from "weakref";
4550
4651### IterableWeakSet
4752
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+
4858** Interface**
4959
5060``` ts
@@ -59,23 +69,29 @@ class IterableWeakSet<T extends object> implements WeakSet<T>, Set<T> {
5969``` ts
6070const set = new IterableWeakSet ();
6171
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: " hey@wan2.land" };
75+ set .add (user );
6476}
77+ // end of scope, user will be garbage collected
6578
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 ();
6882}
6983
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
7586```
7687
7788### IterableWeakMap
7889
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+
7995** Interface**
8096
8197``` ts
@@ -90,23 +106,30 @@ class IterableWeakMap<K extends object, V> implements WeakMap<K, V>, Map<K, V> {
90106``` ts
91107const map = new IterableWeakMap ();
92108
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: " hey@wan2.land" };
112+ const metadata = { created: new Date () };
113+ map .set (user , metadata );
95114}
115+ // end of scope, user will be garbage collected
96116
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 ();
99120}
100121
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
106124```
107125
108126### InvertedWeakMap
109127
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+
110133** Interface**
111134
112135``` ts
@@ -121,17 +144,18 @@ class InvertedWeakMap<K, V extends object> implements Map<K, V> {
121144``` ts
122145const map = new InvertedWeakMap ();
123146
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: " hey@wan2.land" };
150+ map .set (user .id , user );
126151}
152+ // end of scope, user will be garbage collected
127153
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 ();
130157}
131158
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
137161```
0 commit comments