binaryen
Compiler infrastructure and toolchain library for WebAssembly
Binaryen is a compiler toolset for WebAssembly, it converts code into Wasm, optimizes it to be smaller and faster, and provides CLI tools for working with Wasm files directly.
Binaryen
Binaryen is a toolset that makes it easier and faster to create WebAssembly—a technology that lets code run at near-native speed in web browsers and other environments. Think of it as a specialized factory for building and optimizing WebAssembly programs.
At its core, Binaryen does three main things. First, it accepts code in various formats (from other programming languages, from WebAssembly itself, or as a control flow graph that describes how a program branches and loops) and translates it into WebAssembly. Second, it runs dozens of optimization passes on that code—these are like a series of filters that make the WebAssembly smaller and faster, similar to how JavaScript minifiers shrink code for the web. Third, it provides command-line tools that let you read WebAssembly files, optimize them, convert them to JavaScript, or even run them in an interpreter. Binaryen is written in C++ but can also be used from JavaScript.
The project is used by companies and language creators building compilers for WebAssembly. For example, AssemblyScript uses Binaryen to compile a subset of TypeScript, Grain uses it to compile the Grain language, and Asterius uses it to compile Haskell. Emscripten (a popular tool for converting C and C++ code to WebAssembly) relies heavily on Binaryen. The tools that Binaryen provides—wasm-opt for optimization, wasm2js for conversion to JavaScript, wasm-reduce for debugging—are used by anyone needing to work with WebAssembly files directly.
What makes Binaryen special is its internal design: it uses a tree-structured intermediate language (called Binaryen IR) that's very close to WebAssembly itself, which means it can convert between them quickly without losing information. It's also built for parallel optimization—it can use all available CPU cores at once. The optimizer includes sophisticated passes like live-range analysis for register allocation, constant folding, function inlining, and dead-code elimination, all tuned specifically for making WebAssembly lean and fast.
Where it fits
- Build a compiler that targets WebAssembly, like AssemblyScript or Grain do.
- Shrink and speed up existing WebAssembly output using wasm-opt.
- Convert a WebAssembly file to JavaScript using wasm2js.
- Debug a WebAssembly issue by minimizing a failing case with wasm-reduce.