JS RuntimeBaseline · Widely

ES Modules and Live Bindings

Mental Model

ES Module imports do not copy values; they link read-only pointers straight into the exporting module's Execution Context slot. Because linking completes before execution, mutations in the exporter surface immediately to every importer, and static graphs enable tree-shaking.

Interactive Simulator

CommonJS Value Copy vs ESM Live Binding

Lines of Code
Before
6
After
6
Reflows / Browser Cost
Before
Dynamic runtime require
After
Static pre-link graph
BeforeCommonJS (Value Snapshot)
// counter.cjs
let count = 0;
module.exports = { count, inc: () => ++count };
// main.cjs
const { count, inc } = require('./counter.cjs');
inc(); // count is still 0 (copied primitive)
Lines: 6Reflows: Dynamic runtime requireDependencies: Frozen at require() time

Destructured primitives go stale immediately after mutation.

After (Native)ES Modules (Live Read-Only Binding)
// counter.mjs
export let count = 0;
export const inc = () => ++count;
// main.mjs
import { count, inc } from './counter.mjs';
inc(); // count reads 1 via live slot
Lines: 6Reflows: Static pre-link graphDependencies: Zero-copy pointer slot

Importer always dereferences the exporter's live LexicalEnvironment slot.

Under the Hood

During instantiation, V8 allocates a ModuleEnvironmentRecord per module and wires read-only indirect bindings straight to the exporter's slots before execution. Whereas CommonJS require() copies a value snapshot at call time, an ESM import exposes live updates without allowing reassignment.

  1. Construction: The loader fetches and parses all static import specifiers before running any code.
  2. Instantiation: The engine allocates a ModuleEnvironmentRecord for each module and wires indirect bindings from imports to exports.
  3. Evaluation: Modules execute post-order (leaves first); top-level await pauses parent evaluation until settled.
  4. Network Discovery: Unlike <script> tags spotted by Preload Scanner Hints, deep ESM imports trigger waterfalls unless declared with modulepreload.
JS
// counter.mjs
export let count = 0;
export const increment = () => { count += 1; };

// consumer.mjs
import { count, increment } from './counter.mjs';
increment();
console.log(count); // 1 (live read-only view)

Problem Genealogy

  • 2009 (CommonJS & AMD): Synchronous require() copied module.exports by value, breaking circular references and static dead-code elimination.
  • 2015 (ES2015 Modules): Standardized static import/export syntax with live read-only bindings.
  • 2022 (<link rel="modulepreload"> & Import Maps): Eliminated bare-specifier tooling lock-in and sequential fetch waterfalls.

Production Pitfalls

Production Pitfall
  • Circular TDZ Crashes: Reading an imported const before the exporting module finishes evaluating throws ReferenceError.
  • Unbundled Waterfall Latency: Serving 200 raw ESM files over HTTP/2 without modulepreload stalls First Interactive behind serial round-trips.
When Not to Use
  • When not to use raw unbundled ESM: Never ship hundreds of unbundled node_modules ESM files to production browsers without bundling or <link rel="modulepreload"> hints.

Connections & Logical Next Steps

Seen in Architecture Autopsies

Mechanical Comparisons