explaingit

webassembly/binaryen

8,454WebAssemblyAudience · developerComplexity · 5/5Setup · hard

TLDR

C++ library and toolkit for compiling and optimizing code to WebAssembly, used internally by Emscripten, Rust, Kotlin, and other languages that target the browser.

Mindmap

mindmap
  root((binaryen))
    What it does
      Compile to WebAssembly
      Optimize size and speed
      Convert wasm to JS
    Tools shipped
      wasm-opt
      wasm2js
      Interpreter
    Used by
      Emscripten
      Rust wasm-pack
      Kotlin and Dart
      AssemblyScript
    Internals
      Tree IR representation
      Optimization passes
      Dead code removal
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Optimize the size or speed of a WebAssembly binary file using the wasm-opt command-line tool.

USE CASE 2

Integrate Binaryen as a C++ library inside a new compiler or language that targets WebAssembly.

USE CASE 3

Convert a WebAssembly module back to JavaScript using wasm2js for environments that do not support WebAssembly natively.

Tech stack

C++WebAssembly

Getting it running

Difficulty · hard Time to first run · 1h+

Building from source requires a C++ toolchain, using the pre-built wasm-opt binary is much faster to get started.

In plain English

Binaryen is a C++ library that helps other tools compile code to WebAssembly. WebAssembly is a low-level format that runs in web browsers and other environments at near-native speed, and Binaryen handles the difficult work of converting code into that format and then optimizing it for size and performance. Most developers will not use Binaryen directly. Instead, it works as an internal component inside tools they may already know. When you compile C or C++ for the browser using Emscripten, Binaryen produces the final WebAssembly output. When you compile Rust using wasm-pack, Binaryen runs in the background. Kotlin, Dart, and OCaml also route their WebAssembly output through Binaryen. The main command-line tool it ships is called wasm-opt, which reads a WebAssembly file, applies optimization passes, and writes a smaller or faster result. Binaryen also supports languages that compile directly to WebAssembly using it as a library, such as AssemblyScript (a TypeScript-like language designed for WebAssembly). It includes an interpreter for running and testing WebAssembly code, and a converter called wasm2js that turns WebAssembly modules back into JavaScript for environments without native WebAssembly support. Internally, Binaryen represents code as a tree structure rather than the stack-based format WebAssembly uses at the binary level. This tree representation makes it easier to apply many optimization passes, such as dead code removal, function inlining, and WebAssembly-specific size reduction analogous to how JavaScript minifiers work. The full README is longer than what was shown.

Copy-paste prompts

Prompt 1
I have a .wasm file and want to reduce its size for faster web page loading. Give me the wasm-opt command that applies the most common size-reduction passes.
Prompt 2
I am building a language that compiles to WebAssembly and want to use Binaryen as a library. How do I represent a simple function in Binaryen's tree IR and emit a .wasm file?
Prompt 3
How does wasm2js work, and when should I use it instead of just shipping the .wasm file directly?
Prompt 4
I want to run a WebAssembly module in Binaryen's interpreter for testing. What command do I use and what file formats does it accept?
Open on GitHub → Explain another repo

← webassembly on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.