-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathWeakRef.jsdoc
More file actions
50 lines (37 loc) · 1.39 KB
/
Copy pathWeakRef.jsdoc
File metadata and controls
50 lines (37 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
WeakRef : Object
A WeakRef allows holding on to another object, while still allowing that object
to be garbage collected if there are no other strong references to it. Note,
JavaScript makes no guarantees on if or when an object will be garbage collected.
See also %%/FinalizationRegistry|FinalizationRegistry%%.
Spec:
https://tc39.es/ecma262/#sec-weak-ref-objects
Version:
ECMAScript 2021
----
new WeakRef(target : Object) : WeakRef
Creates a new WeakRef that tracks **target**.
Spec:
https://tc39.es/ecma262/#sec-weak-ref-target
----
prototype.deref() : Object
Returns the target of **this** if it has not been garbage collected yet, otherwise
returns **undefined**. If **deref()** returns a valid object, that object
will not be garbage collected until control returns to the event loop, such as
when returning from a callback or **await**ing a %%/Promise|Promise%%.
<example>
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
let foo = {};
const weakRef = new WeakRef(foo);
foo = undefined; // Clear strong reference
const startTime = Date.now();
console.log('Allocating a lot of objects to force garbage collection');
while (weakRef.deref()) {
for (let i = 0; i < 100; i++) {
const x = new Array(100);
}
await sleep(10);
}
console.log(`foo was reclaimed after ${((Date.now() - startTime) / 1000).toFixed(1)}s`);
</example>
Spec:
https://tc39.es/ecma262/#sec-weak-ref.prototype.deref