-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex3.js
More file actions
53 lines (43 loc) · 1.76 KB
/
ex3.js
File metadata and controls
53 lines (43 loc) · 1.76 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
50
51
52
53
'use strict';
/*
Problem:
In real applications, a common leak pattern is accidentally retaining large objects in
long-lived closures (handlers stored in registries, event listeners, etc.).
Implement `createHandler(makeLarge)` that returns an object:
{ handle, release, retainedSize }
Where:
- `makeLarge()` returns a large payload (e.g., an array or string).
- The starter code incorrectly retains the entire payload forever.
- Your goal is to avoid retaining the large payload while still computing the result.
Contract:
- `handle(input)` returns a deterministic value derived from the large payload and input.
For this exercise, the derived value is:
input + summary
where `summary` is the length of the payload returned by makeLarge().
- `retainedSize()` returns how much data is still strongly retained by the handler:
- It MUST return 0 after `release()` is called.
- It MUST be small even before `release()` (do NOT retain the full payload).
Retaining just the summary (a number) is acceptable.
Constraints:
- Do NOT rely on GC, WeakRef, FinalizationRegistry, or heap measurements.
- This is about reachability/retainers: do not keep strong references unnecessarily.
- Tests must pass deterministically.
Starter code is intentionally wrong.
*/
function createHandler(makeLarge) {
const payload = makeLarge(); // WRONG: retains the entire payload
return {
handle(input) {
return input + payload.length;
},
release() {
// TODO: drop references so retainedSize() becomes 0
},
retainedSize() {
// TODO: return an estimate of how much is still strongly retained.
// Returning payload.length is WRONG because it implies full retention.
return payload.length;
}
};
}
module.exports = { createHandler };