What is Binaryen?

Key takeaways:

  • Binaryen is an open-source library for optimizing and transforming WebAssembly code, enabling the creation of efficient modules for web applications.

  • It applies various optimizations to improve performance, reduce the size, and enhance the execution speed of WebAssembly modules.

  • Binaryen validates modules to ensure integrity, preventing runtime errors and security vulnerabilities.

  • It can generate WebAssembly code from higher-level languages and supports transformations between different WebAssembly formats.

  • Key tools include wasm-as for converting .wat to .wasm, wasm-dis for the reverse operation, wasm-opt for various optimizations, and wasm2js for converting to JavaScript.

Binaryen is an open-source library used to optimize and transform WebAssembly code. It provides tools designed to manipulate WebAssembly binary code efficiently. Binaryen aims to create highly performant and efficient WebAssembly modules that can be seamlessly integrated into web applications.

Features and capabilities of Binaryen

  • Optimizations: Binaryen optimizes the code to various levels that can significantly improve the performance of WebAssembly modules. By applying these optimizations, we can reduce the size and enhance execution speed.

  • Validation: The Binaryen validates the module and checks the integrity of the binary code. It ensures the prevention of runtime errors and security vulnerabilities.

  • Code generation: Binaryen can generate the WebAssembly code from higher-level languages and intermediate representations.

  • Wasm-to-Wasm transformations: Besides optimizations, Binaryen facilitates transformations between different forms of WebAssembly code. This includes converting code to a more compact format and making faster loading times for web applications.

Binaryen tools

Below is the list of Binaryen tools that are commonly used:

  • wasm-as: The wasm-as tool takes a textual formate of WebAssembly code (.wat formate) and converts it into binary WebAssembly modules.

wasm-as <input.wat> -o <output.wasm>

Note: <input.wat> and <output.wasm> are input and output files, and the -o flag specifies the output file.

  • wasm-dis: The wasm-dis tool performs the reverse operation as the wasm-as tool does. It takes a binary WebAssembly module and generates the corresponding textual representation in .wat format.

wasm-dis <input.wat> -o <output.wasm>
  • wasm-opt: The wasm-opt is a command-line tool that performs a variety of optimizations on WebAssembly code. It can eliminate dead code, simplify expressions, perform inlining, and more, resulting in smaller and faster-executing modules.

Note: We can also pass the optimization level (e.g. --inlining-optimizing) to minimize the size and execution time.

wasm-opt <input.wasm> -o <output.wasm>
  • wasm2js The wasm2js tool converts WebAssembly textual format to JavaScript format.

wasm2js <input.wat> -o <output.js>

Try yourself

The above command can be tested in the terminal below:

Note:

  • We have added a file add.wat in the below terminal as an input file. You can view it by running the cat add.wat command.

  • Here is the list of commands that you can run in the below playground.

wasm-as add.wat -o add.wasm ## convert the .wat file to .wasm file
wasm-dis add.wasm -o add_dis.wat ## convert the .wasm file to .wat file
wasm-opt add.wasm -o add_optimized.wasm --inlining-optimizing ## optimized the .wasm file
wasm2js add.wat -o add.js ## convert the .wat file to .js file
  • You can run the command like wasm-as add.wat -o add.wasm. The generated file can be listed by running the ls command.

Terminal 1
Terminal
Loading...

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we install Binaryen?

Binaryen can be installed through package managers or building from sources available on GitHub.


What is the difference between wasm-as and wasm-dis?

wasm-as converts textual WebAssembly to binary, while wasm-dis converts binary back to textual format.


What compiler does Emscripten use?

Emscripten uses the LLVM/Clang compiler. It compiles C and C++ source code into WebAssembly, enabling developers to run native code in web browsers.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved