Skip to content

Instantly share code, notes, and snippets.

@bmeck
Last active May 10, 2021 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmeck/0f3280d0ace372dc1af3b417d7a5da39 to your computer and use it in GitHub Desktop.
Save bmeck/0f3280d0ace372dc1af3b417d7a5da39 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// call using --inspect brk, GC using memory tab of devtools
// call globalThis.tick() to move the code along and see when things reap from
// manually calling GC
{
const registry = new FinalizationRegistry((_) => console.log(_, 'reaped'));
let EASY_TO_FIND = class EASY_TO_FIND2 {
big = new Uint8Array(1024 * 1024 * 10);
constructor(_) {
registry.register(this, _);
}
};
function fn() {
let a = new EASY_TO_FIND('fn');
let tasks = [() => ({ fn: a }), () => a = null, () => {}];
return {
next() {
let handler = tasks.shift();
console.log('fn', `${handler}`)
if (handler) return { value: handler(), done: false };
return { value: undefined, done: true };
},
};
}
function generator() {
let iter = (function* generator() {
let a = new EASY_TO_FIND('generator');
yield { generator: a };
console.log('no more generator refs');
yield null;
console.log('generator still has no refs');
yield null;
console.log('generator closing');
return;
})();
iter.next();
return iter;
}
function async() {
/**
* @type {Array<Function | null>}
*/
let ret = [null, null];
new Promise((f) => {
ret[0] = f;
});
new Promise((f) => {
ret[1] = f;
});
new Promise((f) => {
ret[2] = f;
});
(async function async() {
let a = new EASY_TO_FIND('async');
await Promise.all([{ async: a }, ret[0]]);
console.log('no more async refs');
await ret[1];
console.log('async manually cleaning');
a = null;
await ret[2];
console.log('async closing');
return;
})();
return {
next() {
let handler = ret.shift();
if (handler) return { value: handler(), done: false };
return { value: undefined, done: true };
},
};
}
console.log('init');
let cbs = fn();
let iter = generator();
let proms = async();
let ticks = 1;
function tick() {
console.log('starting tick', ticks);
for (let _ of [cbs, iter, proms]) {
_.next();
}
ticks++;
}
// manually run tick() from devtools!
globalThis.tick = tick;
setInterval(() => {}, 1e4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment