-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.js
More file actions
45 lines (35 loc) · 1.12 KB
/
ex2.js
File metadata and controls
45 lines (35 loc) · 1.12 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
'use strict';
/*
Problem:
Implement `simulateGenerationalGC(objects, cycles, promotionAge)`.
Input:
- `objects`: array of objects like { id: 1, age: 0 }
- `cycles`: number of GC cycles to simulate (default 1)
- `promotionAge`: promote to old generation when age >= promotionAge (default 2)
Rules per cycle:
1. Increment age of all young-generation objects.
2. Promote objects whose age is at/above threshold.
3. Promoted objects must be removed from young generation.
4. No object can exist in both young and old at the same time.
Output:
- Return { young: [...], old: [...] }
Starter code is intentionally incorrect: promotion rules and movement semantics
are wrong.
*/
function simulateGenerationalGC(objects, cycles = 1, promotionAge = 2) {
if (!Array.isArray(objects)) {
throw new TypeError('objects must be an array');
}
const young = objects.map((obj) => ({ ...obj }));
const old = [];
for (let i = 0; i < cycles; i++) {
for (const obj of young) {
obj.age += 1;
if (obj.age > promotionAge) {
old.push(obj);
}
}
}
return { young, old };
}
module.exports = { simulateGenerationalGC };