DEV Community

Sascha Dewad for fsharp-wasm

Posted on Edited on

Compiling F# Directly to WebAssembly GC Without the .NET Runtime

How fsharp-wasm turns F# into 4–45 KB WasmGC modules—and what we built across 19 open-source repositories.

For years, running F# in the browser or on edge platforms has come with a familiar trade-off: you ship the entire .NET runtime alongside your code. Solutions like Blazor and Uno are capable, but they carry multi-megabyte payloads and noticeable startup overhead—both of which hurt when you are targeting fast web apps or resource-constrained edge isolates.

With modern WebAssembly features now standardized across major engines—specifically WasmGC, Tail Calls, Exception Handling, and JS String Builtins—compilers for functional languages no longer need to bundle their own garbage collector or linear-memory runtime.

Over the past months, I have been building fsharp-wasm, an experimental open-source compiler and ecosystem (licensed under Apache-2.0) that compiles F# directly into WasmGC binaries without the .NET runtime or Base Class Library (BCL).


How the Compiler Works

Instead of rewriting an F# type-checker from scratch, fsharp-wasm takes a forked Fable frontend, replaces the JavaScript backend with a custom WebAssembly Text (WAT) printer and binary encoder, and pairs it with fsharp-wasm-core—a lightweight replacement for FSharp.Core mapped directly to WasmGC structs, arrays, and unboxed primitives.

Every compiled module targets four modern WebAssembly capabilities directly:

  • WasmGC: Maps F# records, discriminated unions, closures, and arrays directly to host-managed GC structs and arrays.
  • Tail Calls: Compiles recursive functional patterns into proper tail calls so deep recursion never overflows the stack.
  • Exception Handling: Uses native WebAssembly structured exceptions rather than slow JavaScript try/catch trampoline glue.
  • JS String Builtins: Performs string operations directly across the JavaScript/WebAssembly boundary without copying bytes back and forth through linear memory.

Benchmarks and Verification

Because a compiler is only as useful as its correctness and output efficiency, every optimization in fsharp-wasm is measured against both JavaScript and standard .NET:

  • Binary Size: Complete interactive demo applications compile down to 12–153 KB uncompressed (4–45 KB Brotli-compressed).
  • Runtime Speed: Across 13 benchmarks against hand-written JavaScript, F# on V8 is faster in 9 to 11 cases, achieving a geometric mean speedup of 1.45–1.58× (and 1.03× on JavaScriptCore).
  • Differential Correctness: The compiler is validated by 675 regression tests and 183,012 differential test runs comparing WasmGC output directly against standard .NET execution with zero discrepancies.
  • Host Boundary Throughput: Adding a bulk string transfer path across the JS/Wasm boundary recently cut state-transfer overhead for 5,000 Todo items on Node from 20.6 ms down to 6.2 ms.

Inside the Ecosystem (19 Repositories)

Beyond the core compiler (fsharp-wasm), standard library (fsharp-wasm-core), and browser interop runtime (fsharp-wasm-runtime), the organization spans graphics, developer tools, edge adapters, and a planned desktop/mobile shell—all written in F# and compiled to WasmGC:

  • 2D Graphics & Text: fsharp-wasm-graphics implements a retained-mode 2D scene graph paired with an Elm-style (TEA) runtime, while fsharp-glyph-aura handles zero-allocation OpenType text shaping and Signed Distance Field (SDF) text layout.
  • Tools & Applications: fsharp-wasmecto provides an Expecto-style test framework running natively inside WasmGC; fsharp-wasm-ide is a visual widget editor; and fsharp-wasm-newsletter is a browser-based HTML email designer generating Bootstrap 5.3 HTML, web views, and plain-text output.
  • Backend & Edge (fsharp-wasm-edge): An identical F# backend module runs across six JavaScript/Wasm runtimes—Node, Bun, Deno, Cloudflare Workers (including Durable Objects), Vercel, and Netlify.
  • Desktop & Mobile Shell (fsharp-wasm-embedded): A planned native WebView host shell targeting WebView2 (Windows), WKWebView (macOS/iOS), WebKitGTK (Linux), and Android System WebView, paired with a device capability probe and a browser-based WasmOS demo.

Honest Status: What Runs Today vs. What Is Next

fsharp-wasm is an active, experimental engineering project rather than a drop-in production replacement for .NET. Here is where things stand today:

  • Edge Deployments: Cloudflare Workers (including Durable Objects) is deployed live. The Vercel and Netlify adapters pass in our test matrix and are wired up, though their live endpoints have not been verified yet.
  • Embedded Shell: The repository currently ships the WebView capability probe and WasmOS demo; the native OS WebView host wrappers themselves remain to be built.
  • Email Client Testing: The newsletter designer generates clean HTML and plain-text output, though it has not yet been tested across real desktop and mobile mail clients.

On the roadmap next:

  • Multi-App Worker Isolation: Isolating multiple F# applications inside the universal shell/runtime via Web Workers and Module Workers—complete with message-passing IPC, lifecycle management, and fault isolation.
  • Build-Time Tree-Shaking: A pre-compiler pass to strip unused scene-graph widgets and font glyphs at build time so binaries stay small as apps grow.
  • Compile-Time Builder & DSL Optimizations: Eliminating intermediate allocations in custom F# computation expressions and UI builders.
  • Semantic HTML Accessibility: Rendering an accessible, keyboard- and screen-reader-friendly HTML layer alongside the 2D Canvas stack.
  • WIT & Rust Interop (fsharp-wasm-sci): Adopting WebAssembly Interface Types (WIT) to bind existing Rust data-science crates instead of rewriting numerical primitives from scratch.

Get Involved or Support the Project

fsharp-wasm is open-source under the Apache-2.0 license and built independently. If you are interested in compilers, WebAssembly GC, or functional programming on the web, try the live demos at fsharp-wasm.github.io or explore the repositories on github.com/fsharp-wasm.

  • Contribute: Pull requests, benchmark feedback, and testers are especially welcome around native WebView shells, WIT/Rust bindings, edge deployments, and email-client rendering tests.
  • Sponsor or Collaborate: If you or your engineering team want to support ongoing WasmGC compiler development, you can sponsor the project via GitHub Sponsors ( [github.com/sponsors/fsharp-wasm](https://github.com/sponsors/fsharp-wasm) ). I am also available for F#, WebAssembly, and compiler architecture consulting.

Top comments (0)