Skip to content

Instantly share code, notes, and snippets.

@leihuang23
Last active August 18, 2020 07:22
Show Gist options
  • Save leihuang23/1e0dfed84db4264424926dc0b9bd88d0 to your computer and use it in GitHub Desktop.
Save leihuang23/1e0dfed84db4264424926dc0b9bd88d0 to your computer and use it in GitHub Desktop.
Tail call optimization
let timeout = null;
const queue = new Set();
function process() {
for (const task of queue) {
task();
}
queue.clear();
timeout = null;
}
function enqueue(task) {
if (timeout === null) timeout = setTimeout(process, 0);
queue.add(task);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment