Glossary termProgramming construct

Event Loop

Also known as: Message loop, Run loop, Message dispatcher

Programming construct that repeatedly waits for events, such as user input or completed network requests, and dispatches each to its handler.

An event loop is a loop at the heart of many programs that waits for something to happen and then runs the code registered to deal with it. Events can be mouse clicks, key presses, timers firing, data arriving on a network connection or a file read completing. The loop takes the next event from a queue, calls the matching handler, waits for that handler to finish and then moves on to the next event. Graphical user interfaces, games, web browsers and many servers are organized this way.

In web browsers and in Node.js, the event loop is the mechanism that lets single-threaded JavaScript manage many operations at once. Slow tasks such as network requests are handed off to the browser or operating system; when they finish, their callbacks are placed in a queue, and the loop runs them one at a time once the call stack is empty. JavaScript distinguishes ordinary tasks from microtasks, such as the reactions to resolved promises, which run before the next task. Node.js builds its loop on libuv, a C library that wraps the operating system's facilities for non-blocking input and output.

The model's simplicity is also its main constraint. Because handlers run one after another on a single thread, any handler that takes a long time blocks everything else: a heavy calculation in browser code freezes the page, and a slow synchronous function in a Node.js server delays every other request. The usual remedies are to keep handlers short, split long work into pieces or move it to worker threads or separate processes. Event loops are a common foundation for asynchronous programming and an alternative to running one thread per task, as discussed under concurrency.

Key points

  • An event loop repeatedly takes the next event from a queue and runs the handler registered for it.
  • Browsers and Node.js use an event loop so single-threaded JavaScript can manage many pending operations.
  • A long-running handler blocks the loop and delays every other event waiting in the queue.
  • Worker threads or separate processes are typical ways to keep heavy computation off the main loop.

Related entries

Glossary term

Programming approach that lets a program start long-running operations, such as network requests, and continue other work until the results arrive.

Glossary term

Concurrency

Programming

Ability of a program or system to make progress on several tasks during overlapping time periods, whether or not they literally run simultaneously.

Technology

JavaScript

Programming

High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.

Technology

Node.js

Programming

Open-source JavaScript runtime built on Google's V8 engine that runs JavaScript outside the browser using an event-driven, non-blocking model.

Glossary term

Game Engine

Video GamesProgramming

Software framework that provides the core systems for building games, such as rendering, physics, audio, input, scripting and editing tools.

Glossary term

WebSocket

Programming

Communication protocol that provides a persistent, two-way channel between a web browser and a server over a single TCP connection.

Glossary term

Programming interface that represents a web page as a tree of objects, letting scripts read and change its content, structure and style.

Referenced by

Other entries that discuss or point to Event Loop.