Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will web assembly binaries be smaller than JavaScript/TypeScript bundles?

Tags:

webassembly

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
like image 949
David Alsh Avatar asked Jun 09 '20 02:06

David Alsh


People also ask

Is WebAssembly faster than JS?

WebAssembly is a low-level assembly language that can run in any major browser. It's designed to be fast, much faster than JavaScript, in order to handle the modern use cases of the web that require higher performance.

How much faster is WebAssembly than JavaScript?

JavaScript beats WebAssembly one more time. However, the difference is only 1.5 times at this run. We made the computation complicated and the difference got smaller. Maybe, we make the computation way complicated than we can see different results.

Is Wasm smaller than JS?

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.

Is WebAssembly going to replace JavaScript?

Still, WebAssembly will allow developers to move intensive UI-blocking work outside of the browser. It will result in better performing web applications that run on newer and older hardware more smoothly. So, to answer the question (if the answer wasn't obvious enough), WebAssembly will not replace Javascript.


1 Answers

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 aadd 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.

like image 99
ColinE Avatar answered Sep 17 '22 08:09

ColinE