liveBook · Manning

JavaScript runtime

As explained in JavaScript in Depth

An execution environment that runs JavaScript and provides the host APIs needed by its implementation.

The same code, a different hello

A script queues callbacks with process.nextTick(), queueMicrotask(), a resolved promise, setTimeout(), and setImmediate(). Node.js prints “Hello” when the file is CommonJS. Change only its extension to .mjs, and the output changes. Deno and Bun produce still other results; Deno also requires an explicit setImmediate import. The JavaScript statements are not enough to predict the output. section 1.1

A JavaScript runtime is an application that embeds and controls a JavaScript engine. The engine parses, compiles, and executes code. The runtime supplies access to the outside world—such as networking, files, and timers—and decides when JavaScript runs in response to events and which resources are available. Node.js, Deno, Bun, and web browsers are runtimes, but they do not all use the same engine. section 1.3

To explain a behavior, separate three sources of rules:

  • Language-defined behavior comes from ECMAScript and is expected to work the same across runtimes; promises are an example. section 1.3.3
  • Implementation-defined behavior belongs to an engine. Runtimes sharing an engine can be expected to behave roughly alike at this level, though their hosts may differ. section 1.3.3
  • Host-defined behavior belongs to an individual runtime. process.nextTick() and its queue are Node.js-specific, not ECMAScript or V8 features. section 1.3.3

Queues make the distinction visible. JavaScript defines a microtask queue used for promises, but a host chooses when to drain it. Node.js also supplies nextTick and immediates queues and decides when to process them. Do not assume that tasks from one queue always finish before tasks from another, or that another runtime will schedule them the same way. Module loading matters too: Node.js loads an ECMAScript module asynchronously, placing its code in a different scheduling context from the synchronous CommonJS version. section 1.3.2 section 1.1

Table 1.1 Running the same code in five different runtimes yields six different results
Runtime
Variation
Results
Node.js
CommonJS
Hello
Node.js
ECMAScript module
elHo
l
Deno
ECMAScript module
elHlo
Bun
ECMAScript module
Helo
L
Chrome
CommonJS
Error! process.nextTick does not exit.
Cloudflare Workers
ECMAScript module
eHllo

Compare the outputs and the browser error before attributing the puzzle to a single, universal event loop. section 1.1

When behavior changes across deployments, first check the module format, available host APIs, and scheduling queues; then ask whether the difference belongs to the language, engine, or host. Diagnostic tools are runtime-specific, but an understanding of garbage collection, optimization, and scheduling can guide investigation even when a particular runtime lacks the desired trace or monitor. section 1.1 section 11.5

How JavaScript in Depth covers it

How deep it goes
Defines engine and host responsibilities, then traces scheduling differences to queues and module loading; defers detailed queue behavior.
Examples
Compares a queued-callback script under Node.js CommonJS and ECMAScript modules, Deno, Bun, Chrome, and Cloudflare Workers.
What it assumes
Defines engines and host runtimes from first principles while using promises, timers, and module formats as motivating examples.
What you can do after
Diagnose/debug with it by separating host scheduling and API differences from language and engine behavior.
How it connects
Connects ECMAScript, JavaScript engines, promises, microtasks, event loops, and module loading through their distinct roles in execution.

Also explained in

Compare how 6 books explain JavaScript runtime →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.