logoalt Hacker News

charcircuitlast Friday at 10:08 AM2 repliesview on HN

>WebAssembly is not huge

I always feel like I'm downloading megabytes of it whenever someone uses it. In practice it is. Even a basic hello world in rust will set you back a few megabytes compared to a the tens of bytes it takes in javascript.

>JavaScript comes with more of a standard library and more of a runtime, which unbalances comparisons.

Being able to make programs in a few bytes is a legitimate strength. You can't discount it because it's an effective way javascript saves size.


Replies

chrismorganlast Friday at 10:46 AM

> Even a basic hello world in rust will set you back a few megabytes

Lies. It’s 35 kB:

  $ cargo new x
  …

  $ cd x

  $ cat src/main.rs
  fn main() {
      println!("Hello, world!");
  }

  $ cargo build --release --target=wasm32-unknown-unknown
  …

  $ ls -l target/wasm32-unknown-unknown/release/x.wasm
  … 34597 …
And that’s with the default allocator and all the std formatting and panic machinery. Without too much effort, you can get it to under 1 kB, if I remember correctly.

For the rest: I mention comparisons being unbalanced because people often assume it will scale at the rate they’ve seen—twice as much code, twice as much size. Runtimes and heavy tables make for non-scaling overhead. That 35 kB you’ve paid for once, and now can use as much as you like without further growth.

show 1 reply
adrian17last Friday at 11:04 AM

> Even a basic hello world in rust will set you back a few megabytes compared to a the tens of bytes it takes in javascript.

That's definitely not true.

A debug build of a "hello wasm-bindgen" style Rust program indeed takes ~2MB, but most of that is debug into; disabling that and/or stripping gets it down to 45-80kB (depending how I did it). And a release build starts at 35kB, and after `wasm-opt -O` gets down to 25kB. AFAIK most of the remaining space is used by wasm-bindgen boilerplate, malloc and panic machinery.

...and then, running wasm-bindgen to generate JS bindings somehow strips most of that boilerplate too, down to 1.4kB.

Side note, I never understood how wasm-opt is able to squeeze so much on top of what LLVM already did (it's a relatively fast post-build step and somehow reduces our production binaries by 10-20% and gives measurable speedups).