feat(core): add Deno.core.loadExtScript() for lazy-loaded scripts#33739
Merged
Conversation
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 = [...]`).
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.
3 tasks
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
Deno.core.loadExtScript(specifier)— a new API for loading extensionscripts 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/exportsemantics, using scriptsinstead of ESM can significantly reduce snapshot size and improve startup time.
What's included:
lazy_loaded_jsparameter in theextension!macro (analogous tolazy_loaded_esm)op_load_ext_scriptop that evaluates a script and returns the resultDeno.core.loadExtScript(specifier)JS API with JS-side result cachingext/webstorage/01_webstorage.jsfrom ESM to lazy scriptScript convention: Files are manually wrapped in an IIFE, access core via
globalThis.__bootstrap, and usereturnfor exports. Oncedenoland/rusty_v8#1973 lands,
we can switch to automatic IIFE wrapping via
v8::String::Concatto avoidneeding to reformat files.
Test plan
cargo test -p deno_core test_lazy_loaded_script— unit tests passcargo build --bin deno— full binary builds with snapshotdeno eval 'localStorage.setItem("k","v"); console.log(localStorage.getItem("k"))'works