Skip to main content

Motivation

This library was created to simplify the process of leveraging WASM inside of Audio Worklets.

Why WASM?

You might ask yourself: is it really more efficient to take a whole other language, compile it down to bits, and run that inside of JavaScript? And I’d say: in some cases - actually yeah.

Garbage free since 2023

The problem with JavaScript is that it’s not the most memory efficient language. It’s designed to be dynamic and easy to use, so you don’t have to think about finer details like allocating memory.

To handle this, JavaScript has something called a “garbage collector”, where any unused data gets released regularly, allowing us to allocate more as needed (and prevent us from overloading or leaking too much memory).

But when we run WASM code, it doesn’t have a garbage collector, since we write it in a language that allocates the exact amount of memory needed at all times (only recently was it added for supporting GC based languages like PHP). That allows us to run the same simple functions (like taking integers and adding or multiplying them together) and it makes it much faster.

This makes a bigger difference when you consider how we process audio. When we analyze audio, we’re often observing a constant stream of data. And we’re talking a lot of data — often arrays of 1000s of items at a time and looping over each of them. Any performance we can save with those “low level” processes, it’d be nice.

Reusability

You might not be aware, but most audio code isn’t written in JavaScript. It’s usually written in a low level language like C++, maybe Python at the most. This is because C++ (and other languages like Rust) are much better at processing large amounts of data more efficiently.

And to be honest, C and C++ have been around the longest, so their ecosystem is the most mature. You’ll find that most Rust modules are wrappers around existing C libraries.

So to be able to leverage that ecosystem and other libraries is incredibly valuable, instead of having to convert their code to JavaScript (and hope it’s nearly as efficient).

Why Rust

Rust as a programming language has proven itself to be just as powerful as the titans of low level langugages, like C++. It's easy to get started with projects and dependencies thanks to it's Cargo CLI. And it's memory management is next level, with a fantastic first party linter to assist with navigating it's ownership model.

Rust is growing in popularity for several fields, from graphics to game dev to audio programming -- there's new crates releasing and updating constantly to populate and evolve the ecosystem.

And it's becoming a standard, alongside C++, for using with Web Assembly (or WASM). There's plenty of Rust-based tooling that simplifies the process. Which is the primary use case of this library.