Is there a faster way to yield to Javascript event loop than setTimeout(0)?

Yes, the message queue will have higher importance than timeouts one, and will thus fire at higher frequency. You can bind to that queue quite easily with the MessageChannel API: let i = 0; let j = 0; const channel = new MessageChannel(); channel.port1.onmessage = messageLoop; function messageLoop() { i++; // loop channel.port2.postMessage(“”); } function … Read more

What is the relationship between event loop and Promise [duplicate]

Each event loop has a microtask queue and a macrotask queue. A microtask is a task that is originally to be queued on the microtask queue rather than a task queue. Refer to https://www.w3.org/TR/html51/webappapis.html#microtask-queue. There are two kinds of microtasks: solitary callback microtasks, such as Promiseļ¼Œ and compound microtasks, such as Object.observe, MutationObserver and process.nextTick … Read more

Difference between microtask and macrotask within an event loop context

One go-around of the event loop will have exactly one task being processed from the macrotask queue (this queue is simply called the task queue in the WHATWG specification). After this macrotask has finished, all available microtasks will be processed, namely within the same go-around cycle. While these microtasks are processed, they can queue even … Read more