Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

V8 11 min read How-to mediumAdept

Introducing the WebAssembly JavaScript Promise Integration API

JSPI lets WebAssembly code written against synchronous APIs call asynchronous Web APIs. It intercepts Promises returned from wrapped JavaScript imports, suspends the Wasm computation, and resumes it from the browser's event loop when the Promise resolves. Wrapped exports return a Promise instead of a direct value. JSPI changes neither JavaScript nor Wasm, and suspend/resume costs are roughly constant: a deliberately bad Promise-based Fibonacci benchmark puts each JSPI'd call at about 1μs. The API is at phase 4 and ships in Chrome 137 and Firefox 139. Emscripten examples show `-s JSPI` with `EM_ASYNC_JS`, plus a trampoline pattern that lazily fetches and instantiates a Wasm module on first call.

Assumes: WebAssembly basicsJavaScript PromisesC compilation with Emscripten

Related posts

  • WebAssembly JSPI has a new API

    V8 mediumAdept

    WebAssembly JavaScript Promise Integration (JSPI) ships a revised API in Chrome M126 that drops explicit Suspender objects and instead uses the most recent JS-to-Wasm export call as the suspension cut point. The new API consists of WebAssembly.promising() to wrap exports and a WebAssembly.Suspending constructor to mark imports, removing the dependency on the Type Reflection proposal. Suspending imports now only suspend when the JS function actually returns a Promise, which avoids unnecessary event-loop trips. Emscripten 3.1.61+ users need no code changes, the old API is removed after Chrome M128, and V8 is working on growable (segmented) stacks so that millions of suspended coroutines become feasible.

  • Why is WebAssembly a second-class language on the web?

    Mozilla Hacks mediumAdept

    WebAssembly remains second-class on the web because it can only load code and reach Web APIs through JavaScript. That forces arcane instantiation boilerplate, language-specific glue code from tools like wasm-bindgen and embind, JS-centric documentation, and real runtime overhead. A 2020 TodoMVC experiment with the Dodrio Rust framework found that removing JS glue cut DOM-update time by 45%. The esm-integration proposal, being implemented in Firefox, simplifies loading, and the WebAssembly Component Model with WIT interfaces is proposed as a standardized artifact that could let Wasm call Web APIs directly with no JavaScript in between.

  • Bringing Javascript to WebAssembly for Shopify Functions

    Shopify Engineering mediumAdept

    Shopify runs JavaScript in Shopify Functions by compiling the QuickJS interpreter to WebAssembly/WASI via Javy. JIT engines like V8 can't run inside Wasm, so QuickJS fills the gap. Embedding the engine with the user's bytecode produced modules of 800KB or more, well over the 256KB limit. Shopify got around this with an ad-hoc dynamic linking scheme built on Wasmtime's linker: a shared "javy_quickjs_provider_v1" library exposes realloc and eval_bytecode, so each function shrinks to a roughly 220-byte stub plus its bytecode, and the engine can be precompiled and patched centrally. The minimal Javy runtime adds POSIX-like stdin/stdout APIs on a Javy global and a Rust-implemented UTF-8 TextEncoder/TextDecoder, but does not yet enable the event loop.

  • Introducing Turso in the Browser

    Turso mediumAdept

    Turso, a from-scratch SQLite rewrite, now runs in the browser via WebAssembly as the npm package @tursodatabase/database-wasm, with a better-sqlite3-like API whose methods are all async because of OPFS. Query compute runs on the main UI thread, since benchmarks showed offloading to a worker cost too much in communication overhead. A Web Worker handles filesystem I/O through the synchronous OPFS interface and shares buffers with the main thread via SharedArrayBuffer, which requires COOP/COEP headers and locks a database to one tab. The code is compiled to wasm32-wasip1-threads with napi-rs v3, and there are dedicated Vite and Turbopack exports plus a CDN build that inlines the WASM as base64 at the cost of a larger bundle.

  • Introducing the JetStream 3 Benchmark Suite

    WebKit hardMaster

    JetStream 3 replaces JetStream 2's separate Wasm Startup/Runtime scores with the JavaScript-style first-iteration, worst-case and average-case scoring. The old scheme broke once engines hit sub-millisecond startup that Date.now() rounded to zero, giving infinite scores. The suite also favors larger workloads over microbenchmarks. WebKit details the JavaScriptCore work behind its results. Inlining WasmGC object fields into one allocation gave about 40% on the WasmGC subtests, and removing per-object destructors gave about 40% on Dart-flute-wasm. Other work inlines bump-pointer allocation into the BBQ and OMG tiers, uses Cohen's display for constant-time subtype checks, makes priority-based non-local inlining decisions, and profiles indirect calls for guarded inlining.

The quest for software engineering knowledge.

Look:
Layout: