Optimizing for Size - The wasm-bindgen Guide (original) (raw)

The `wasm-bindgen` Guide

Optimizing for Size with wasm-bindgen

The Rust and WebAssembly Working Group's Game of Life tutorial has an excellent section on shrinking Wasm code size, but there's a fewwasm-bindgen-specific items to mention as well!

First and foremost, wasm-bindgen is designed to be lightweight and a "pay only for what you use" mentality. If you suspect that wasm-bindgen is bloating your program that is a bug and we'd like to know about it! Please feel free to file an issue, even if it's a question!

What to profile

With wasm-bindgen there's a few different files to be measuring the size of. The first of which is the output of the compiler itself, typically attarget/wasm32-unknown-unknown/release/foo.wasm. This file is not optimized for size and you should not measure it. The output of the compiler when linking with wasm-bindgen is by design larger than it needs to be, thewasm-bindgen CLI tool will automatically strip all unneeded functionality out of the binary.

This leaves us with two primary generated files to measure the size of:

Example

As an example, the wasm-bindgen repository contains an exampleabout generating small Wasm binaries and shows off how to generate a small wasm file for adding two numbers.