CLI tool
AssemblyScript/binaryen.js avatar
AssemblyScript/binaryen.js

binaryen.js: Running Binaryen's WebAssembly Toolchain in the Browser and Node.js

A buildbot for browser & Node.js builds of Binaryen, a compiler infrastructure and toolchain library for WebAssembly.

404 stars54 forksJavaScriptApache-2.0

At a glance

What is it?
binaryen.js is a JavaScript port of Binaryen, the WebAssembly compiler infrastructure. It lets you build, optimize, and manipulate Wasm modules with a JS API, and it ships the command-line tools as Node.js binaries.
Who is it for?
Adopt binaryen.js if you need to generate or optimize WebAssembly from JavaScript, especially in a browser environment where native Binaryen binaries are unavailable. Skip it if you only need to run wasm-opt on a server, where the native Binaryen binaries are faster and easier to update.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 4 days ago.
What is it written in?
Mainly JavaScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 14, 2026, and from our analysis. They are not legal advice.

DEEP OPEN-SOURCE ANALYSIS

What binaryen.js Is For

binaryen.js solves a specific problem: it brings Binaryen, the C++ compiler infrastructure for WebAssembly, to JavaScript. The README describes it as a port to the Web, allowing you to generate WebAssembly using a JavaScript API. That means you can construct a module programmatically, optimize it, and emit binary or text format without leaving the JS ecosystem. The target users are developers building tools that manipulate Wasm at runtime or build time: bundlers, compilers, and anything that needs to transform Wasm in a browser or a Node.js process. If you have ever tried to run wasm-opt in a browser, you know the problem: native binaries do not work there. binaryen.js is the workaround, compiled to WebAssembly itself so it runs anywhere JavaScript runs.

The Module API: Build, Optimize, and Emit

The core of binaryen.js is the Module class. The README gives a concrete example: you create a new binaryen.Module(), add a function with addFunction, and then call myModule.opt() to optimize. The API is a direct mapping of Binaryen's C++ interface, so it uses references like ExpressionRef and FunctionRef. You construct expressions by chaining calls like myModule.i32.add() and myModule.local.get(). The module object also handles imports, exports, and memory. After building, you can emit the result, though the README snippet cuts off before showing the emit call. The API includes parseText to create a module from Binaryen's s-expression format, and readBinary to load from a Uint8Array. This is not a high-level wrapper; it is the full Binaryen API, which means you get fine-grained control but also a steep learning curve if you are new to compiler IR.

Getting It Running: npm and CDN

Installation is straightforward: npm install binaryen. The package also publishes a nightly tag, so npm install binaryen@nightly gets the latest build if there have been changes in the last day. For browser use, the README lists CDN links, for example https://cdn.jsdelivr.net/npm/binaryen@VERSION/index.js. You replace VERSION with a specific release tag, or omit it to get the latest, which the README warns is not recommended for production. The package includes Node.js builds of Binaryen's command-line tools: wasm-shell, wasm-opt, wasm-metadce, wasm2js, wasm-as, wasm-dis, wasm-ctor-eval, wasm-reduce, and wasm-merge. That is a significant convenience: you can run these tools as npm scripts without installing native Binaryen separately.

The Buildbot and the Nightly Release Cycle

The project is described as a buildbot, meaning it is not a hand-maintained port but an automated build that compiles Binaryen to JavaScript. The README mentions it publishes nightly versions once a day if there have been changes. That automation explains the warning in the API section: the Binaryen API is evolving fast, and definitions and documentation provided by the package tend to get out of sync despite our best efforts. It's a bot after all. This is a real trade-off. You get fresh builds with the latest Binaryen features, but you also get the risk that the TypeScript definitions (index.d.ts) lag behind the actual API. The README explicitly asks users to send pull requests to update index.d.ts and README.md to reflect the current API. If you rely on TypeScript types, you may need to check the generated definitions against the actual behavior.

Limitations: API Drift and Feature Support

The most obvious limitation is the API drift just mentioned. The README itself says the definitions and docs 'tend to get out of sync.' That means you cannot trust the type hints or even the README examples to match the installed version. Another limitation is feature support: the README marks several operations with a unicorn emoji, such as atomic memory accesses, exception handling, and reference types. The note says 'Future features might not be supported by all runtimes.' So even if binaryen.js exposes these operations, the resulting Wasm may not run everywhere. Also, the API is low-level. The example shows manual local management: you set a local, get it, and return it. That is verbose compared to higher-level compilers like AssemblyScript itself. If you are generating Wasm from a language, you would not use binaryen.js directly; you would use a front-end that emits the IR.

Alternatives: Native Binaryen and Other Wasm Generators

The natural alternative is the native Binaryen distribution, which you install via a package manager or build from source. The difference is deployment: native binaries run only on the host platform, so you cannot use them in a browser or in a cross-platform npm package without per-platform binaries. binaryen.js trades speed and native performance for portability. Another alternative is to use a JavaScript library that generates Wasm at a higher level, such as wabt.js or the WebAssembly JavaScript API directly. The WebAssembly API lets you instantiate and compile, but it does not offer optimization passes or the rich IR manipulation that Binaryen provides. binaryen.js is the only option in this list that gives you Binaryen's optimizer in a browser. If you need wasm-opt specifically, binaryen.js is the direct answer, but if you just need to compile a simple module, the built-in WebAssembly API may suffice.

Maintenance and License

The project is hosted under the AssemblyScript organization, but the README points to the WebAssembly/binaryen repository as the source. The license is Apache-2.0, which allows commercial use, modification, and distribution, with the usual conditions about retaining copyright notices. Maintenance is automated through the buildbot, but the human maintenance burden falls on the AssemblyScript team and contributors who keep the typings and README current. The last push date is unknown, and no recent releases are listed, which might indicate a lull or just that the buildbot tags releases differently. The README mentions 'previous versions' available via tags, so release tagging is a normal part of the workflow. If you depend on binaryen.js, you should track the nightly builds for fixes, but also pin to a specific version for stability, because the API can change between nightlies.

Editorial conclusion

Adopt binaryen.js if you need to generate or optimize WebAssembly from JavaScript, especially in a browser environment where native Binaryen binaries are unavailable. Skip it if you only need to run wasm-opt on a server, where the native Binaryen binaries are faster and easier to update. Before adopting, verify that the API version matches the Binaryen features you use, since the README warns the definitions can drift. Also check the nightly build for recent fixes if you hit a bug in the stable release.

Official sources

  1. Official documentation
  2. Official README
  3. Project repository
Community notes

Community notes