Debian Codegraph @

.cargo/vendor/wasm-bindgen/guide/src/reference/optimize-size.md

raw
1 # Optimizing for Size with `wasm-bindgen`
2
3 The Rust and WebAssembly Working Group's [Game of Life tutorial][gol] has an
4 excellent section on [shrinking wasm code size][size], but there's a few
5 `wasm-bindgen`-specific items to mention as well!
6
7 First and foremost, `wasm-bindgen` is designed to be lightweight and a "pay only
8 for what you use" mentality. If you suspect that `wasm-bindgen` is bloating your
9 program that is a bug and we'd like to know about it! Please feel free to [file
10 an issue][issue], even if it's a question!
11
12 ### What to profile
13
14 With `wasm-bindgen` there's a few different files to be measuring the size of.
15 The first of which is the output of the compiler itself, typically at
16 `target/wasm32-unknown-unknown/release/foo.wasm`. **This file is not optimized
17 for size and you should not measure it.** The output of the compiler when
18 linking with `wasm-bindgen` is by design larger than it needs to be, the
19 `wasm-bindgen` CLI tool will automatically strip all unneeded functionality out
20 of the binary.
21
22 This leaves us with two primary generated files to measure the size of:
23
24 * **Generated wasm** - after running the `wasm-bindgen` CLI tool you'll get a
25 file in `--out-dir` that looks like `foo_bg.wasm`. This file is the final
26 fully-finished artifact from `wasm-bindgen`, and it reflects the size of the
27 app you'll be publishing. All the optimizations [mentioned in the code size
28 tutorial][size] will help reduce the size of this binary, so feel free to go
29 crazy!
30
31 * **Generated JS** - the other file after running `wasm-bindgen` is a `foo.js`
32 file which is what's actually imported by other JS code. This file is already
33 generated to be as small as possible (not including unneeded functionality).
34 The JS, however, is not uglified or minified, but rather still human readable
35 and debuggable. It's expected that you'll run an uglifier or bundler of the JS
36 output to minimize it further in your application. If you spot a way we could
37 reduce the output JS size further (or make it more amenable to bundler
38 minification), please let us know!
39
40 ### Example
41
42 As an example, the `wasm-bindgen` repository [contains an example][example]
43 about generating small wasm binaries and shows off how to generate a small wasm
44 file for adding two numbers.
45
46 [gol]: https://rustwasm.github.io/book/game-of-life/introduction.html
47 [size]: https://rustwasm.github.io/book/game-of-life/code-size.html
48 [issue]: https://github.com/rustwasm/wasm-bindgen/issues/new
49 [example]: https://rustwasm.github.io/docs/wasm-bindgen/examples/add.html