Skip to content

feat(core): add Deno.core.loadExtScript() for lazy-loaded scripts#33739

Merged
bartlomieju merged 3 commits into
mainfrom
feat/load-ext-script
May 1, 2026
Merged

feat(core): add Deno.core.loadExtScript() for lazy-loaded scripts#33739
bartlomieju merged 3 commits into
mainfrom
feat/load-ext-script

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Summary

Adds Deno.core.loadExtScript(specifier) — a new API for loading extension
scripts on demand without going through V8's ES module infrastructure.

Motivation: Benchmarking showed that ES modules add ~31% snapshot size
overhead and ~4x slower serialization/deserialization compared to plain scripts
(investigation gist).
For extensions that don't need import/export semantics, using scripts
instead of ESM can significantly reduce snapshot size and improve startup time.

What's included:

  • New lazy_loaded_js parameter in the extension! macro (analogous to lazy_loaded_esm)
  • op_load_ext_script op that evaluates a script and returns the result
  • Deno.core.loadExtScript(specifier) JS API with JS-side result caching
  • Circular dependency detection (errors on cycles)
  • Unit tests for loading, caching, dependencies, and error cases
  • Proof-of-concept migration of ext/webstorage/01_webstorage.js from ESM to lazy script

Script convention: Files are manually wrapped in an IIFE, access core via
globalThis.__bootstrap, and use return for exports. Once
denoland/rusty_v8#1973 lands,
we can switch to automatic IIFE wrapping via v8::String::Concat to avoid
needing to reformat files.

Test plan

  • cargo test -p deno_core test_lazy_loaded_script — unit tests pass
  • cargo build --bin deno — full binary builds with snapshot
  • Smoke test: deno eval 'localStorage.setItem("k","v"); console.log(localStorage.getItem("k"))' works

Adds a new `lazy_loaded_js` parameter to the `extension!` macro and a
`Deno.core.loadExtScript(specifier)` API that loads and evaluates
scripts on demand.

This is an alternative to `lazy_loaded_esm` / `createLazyLoader()` that
avoids V8's ES module infrastructure entirely. Scripts loaded this way
don't create module records, namespace objects, or module graph edges,
resulting in smaller snapshots and faster serialization/deserialization.

Scripts are expected to be wrapped in an IIFE and use `return` to
provide exports. They access `core` and `primordials` via
`globalThis.__bootstrap`. Results are cached on the JS side so each
script is evaluated at most once. Circular dependencies are detected
and cause an error.

As a proof of concept, `ext/webstorage/01_webstorage.js` is migrated
from ESM (`esm = [...]`) to lazy script (`lazy_loaded_js = [...]`).
fibibot

This comment was marked as off-topic.

Converts `ext/webidl/00_webidl.js` from ESM (`esm = [...]`) to a
lazy-loaded script (`lazy_loaded_js = [...]`). This removes 1 module
record, 1 namespace object, and all associated module graph edges from
the V8 snapshot.

All 36 consumer files across ext/web, ext/fetch, ext/cache, ext/crypto,
ext/websocket, ext/webgpu, ext/image, ext/node, and runtime/ are updated
to use `core.loadExtScript("ext:deno_webidl/00_webidl.js")` instead of
`import * as webidl from "ext:deno_webidl/00_webidl.js"`. Files that
only imported `primordials` from `ext:core/mod.js` now also import
`core`.
Converts three more ext/web files from ESM to lazy-loaded scripts:

- `02_timers.js` - setTimeout/setInterval (2 consumers updated)
- `01_dom_exception.js` - DOMException class (18 consumers updated)
  Uses lazy loading for console's createFilteredInspectProxy to avoid
  circular dependency with the still-ESM console module.
- `05_base64.js` - atob/btoa (2 consumers updated)

All consumer files across ext/ and runtime/ are updated to use
`core.loadExtScript()` instead of ESM imports.
fibibot

This comment was marked as off-topic.

@bartlomieju
bartlomieju merged commit a09ed54 into main May 1, 2026
136 checks passed
@bartlomieju
bartlomieju deleted the feat/load-ext-script branch May 1, 2026 06:30
bartlomieju added a commit that referenced this pull request May 2, 2026
…33760)

- Converts all 24 ext/web JS files from ESM modules to IIFE-wrapped
lazy-loaded scripts using `core.loadExtScript()`, building on the
infrastructure from #33739
- Updates all external consumers across `ext/`, `runtime/`, and `cli/`
from `import` to `core.loadExtScript()`
- Replaces `createLazyLoader()` calls targeting ext:deno_web files with
`loadExtScript()` since they are now plain scripts

## Notes

- `import.meta.log` in `06_streams.js` replaced with `core.print()` as a
temporary workaround since `import.meta` is not available in non-module
scripts. A proper `core.log` equivalent may be needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants