16

WASM provides a compilation target for languages, enabling them to be compiled such as to be executable inside the browser.

Of course, it's lacking certain features currently - such as direct DOM access from WASM and initializing a binary without using JavaScript.

Ignoring that, the goal of a browser-compatible compile target is satisfied by JavaScript today. However, the output JavaScript will often be convoluted due to it being a high level language itself and often result in an output larger than the source code itself.

Assuming a world where DOM access existed inside wasm, would:

  • Excluding the language runtime, would JavaScript or TypeScript compiled to WASM result in binary sizes smaller than the equivalent JavaScript bundle generated using Webpack?
  • Will runtimes be shared and delivered separately? See Java, SilverLight, Flash, Shockwave

1 Answer 1

18

As you can imagine, this is not a question that can be answered definitely, however, I can give you a better understanding of the current situation and where things are going.

An app compiled to a WebAssembly module, will have the following component parts:

  1. The app logic itself
  2. (optional) A runtime
  3. (optional) Host API integration

Looking at each in turn:

Regarding (1), WebAssembly modules are a size-efficient binary format. For that reason, it is more compact (i.e. smaller) than the equivalent logic represented by minified JavaScript.

Re:2, WebAssembly lacks common runtime features such as (heap) memory management and garbage collectors. For that reason, a runtime is often shipped alongside your application logic. In some cases (Rust) this runtime is quite lightweight, and in others (Blazor) it is very heavy. We will likely see the weight of these runtimes decreasing significantly over time due to new WebAssembly features (garbage collection, module caching) and better compilation techniques (ahead-of time compilation).

Re:3, as you acknowledged, WebAssembly lacks DOM access - in fact is lacks any form of I/O. At the moment your 'standard' WebAssembly tooling generates 'bindings' that add additional weight to your WebAssembly modules and some JavaScript 'glue' code. This will likely decrease over time as initiatives like the interface types proposal gains traction.

So to answer you questions, yes, I do think wasm modules will be more compact than their equivalents in future. I also think the runtimes will be delivered separately and cached, but more importantly this will significantly decrease in size.

3
  • 3
    Thank you for taking the time to write this response, it was exactly what I was looking for.
    – David Alsh
    Commented Jun 10, 2020 at 1:18
  • it is clear, but is there demo model apps (such us blog, tetris, auction etc) implemented in both tech stacks to compare size? Commented Jul 20, 2023 at 14:12
  • I would like to mention Zig language alongside with Rust mentioned in answer. I've tried to keep Rust wasm bundle size small, however, I failed at the moment. With Zig see an example github.com/daneelsan/minimal-zig-wasm-canvas/tree/… of filling canvas with a checkbord with gzipped wasm file of 284 Bytes (580 Bytes uncompressed).
    – kostyfisik
    Commented May 17 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.