LinuxCommandLibrary

wasm-opt

Optimize WebAssembly modules

TLDR

Apply default optimizations and write to a given file

$ wasm-opt -O [input.wasm] [[-o|--output]] [output.wasm]
copy

Apply all optimizations and write to a given file (takes more time, but generates optimal code)
$ wasm-opt -O4 [input.wasm] [[-o|--output]] [output.wasm]
copy

Optimize a file for size
$ wasm-opt -Oz [input.wasm] [[-o|--output]] [output.wasm]
copy

Print the textual representation of the binary to console
$ wasm-opt [input.wasm] --print
copy

SYNOPSIS

wasm-opt [options] input.wasm -o output.wasm

PARAMETERS

-o output.wasm
    Specifies the output file for the optimized Wasm module. If not provided, the input file will be overwritten.

--strip-debug
    Removes debug information from the Wasm module.

-O0
    No optimizations (default).

-O1
    Basic optimizations, good for development.

-O2
    Aggressive optimizations.

-O3
    Very aggressive optimizations, may increase compilation time.

-O4
    Even more aggressive optimizations, may increase compilation time significantly.

-Os
    Optimize for size.

-Oz
    Optimize for size, even more aggressively.

--all-features
    Enable all WebAssembly features (experimental).

--passes=pass1,pass2,...
    Specifies a comma-separated list of passes to run.

--help
    Display help information.

--version
    Display version information.

DESCRIPTION

wasm-opt is a command-line tool from the Binaryen toolkit used to optimize and transform WebAssembly (Wasm) modules. It applies various optimization passes to reduce the size and improve the performance of Wasm code.
It can perform dead code elimination, function inlining, local variable optimization, and more. The specific optimizations applied are determined by the flags provided to wasm-opt. It's a crucial tool in the WebAssembly development pipeline, as it can significantly reduce the size of Wasm modules making them faster to download and execute, especially in web browsers.
It helps producing efficient and compact Wasm binaries, suitable for deployment across different platforms and environments.
The transformation functionalities can modify or enhance the existing features or security measures of Wasm files.

PASSES

Passes are individual optimization steps that wasm-opt applies. There are many passes available, and you can specify which ones to run using the --passes option. Some commonly used passes include remove-unused-module-elements, inlining, local-cse, and reorder-functions.

OPTIMIZATION LEVELS

The -O0, -O1, -O2, -O3, -O4, -Os, and -Oz options specify different optimization levels. Higher optimization levels apply more aggressive optimizations, but may also increase compilation time. It is often a good idea to start with -O1 or -O2 and then experiment with higher levels if necessary.

HISTORY

wasm-opt is part of the Binaryen toolkit, a compiler infrastructure library for WebAssembly. Binaryen was created by Alon Zakai at Google and has been actively developed since the early days of WebAssembly. wasm-opt is crucial for improving the performance and reducing the size of WebAssembly modules before deployment. It is widely used in web development, game development, and other areas where WebAssembly is used to build high-performance applications.

SEE ALSO

wasm-as(1), wasm-dis(1)

Copied to clipboard