Leroy Campbell

Leroy Campbell

Understanding Event Loops by Implementing One

Matt Might once said:

My teaching assumes that to understand is to implement.

So to better understand how JavaScript’s event loop works, I implemented a simplified version (in Go). I only focused on timers; I may add asynchronous I/O later. My implementation combines a priority queue and counter to schedule tasks on the event loop. Calling setTimeout or setInterval adds a task, using its delay to calculate the task’s priority. When the task completes, the loop increments its counter to advance time.

Check out the full implementation on GitHub.