Skip to content

Instantly share code, notes, and snippets.

@kt3k
Last active August 27, 2024 09:17
Show Gist options
  • Save kt3k/b705f858331f061c538033717cedc884 to your computer and use it in GitHub Desktop.
Save kt3k/b705f858331f061c538033717cedc884 to your computer and use it in GitHub Desktop.
let counter = 0;
const registry = new FinalizationRegistry(() => {
console.log(`Array gets garbage collected at ${counter}`);
});
(function allocateMemory() {
// Allocate 50000 functions — a lot of memory!
registry.register(Array.from({ length: 50000 }, () => () => {}));
if (counter > 1000) {
console.log("Main job ends");
return;
}
counter++;
// Use setTimeout to make each allocateMemory a different job
setTimeout(allocateMemory, 10);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment