WebAssembly Explained: Running Native Code in the Browser

WebAssembly (Wasm) quietly solved a problem web developers had accepted as unsolvable: running near-native-speed code in the browser without JavaScript as the only option. It’s now the backbone of everything from Figma’s rendering engine to running full Linux desktop environments in a browser tab.

What It Actually Is

Wasm is a low-level, binary instruction format designed as a compilation target — you write code in C, C++, Rust, or Go, compile it to a .wasm binary, and the browser executes it in a sandboxed virtual machine at speeds close to native code, dramatically faster than equivalent JavaScript for compute-heavy tasks.

Compiling Rust to WebAssembly

cargo install wasm-pack
cargo new --lib wasm-demo
cd wasm-demo
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u64 {
    match n {
        0 => 0,
        1 => 1,
        _ => {
            let (mut a, mut b) = (0u64, 1u64);
            for _ in 2..=n {
                let c = a + b;
                a = b;
                b = c;
            }
            b
        }
    }
}
wasm-pack build --target web
import init, { fibonacci } from "./pkg/wasm_demo.js";

await init();
console.log(fibonacci(50)); // runs at near-native speed

Where Wasm Shines

  • Image/video editing and codecs (Photoshop’s web version runs on Wasm)
  • Games and 3D rendering engines ported to the browser
  • Running existing native libraries (compression, cryptography, physics engines) without a JavaScript rewrite
  • Server-side sandboxed plugin systems, increasingly popular outside the browser entirely (WASI)

What It’s Not For

Wasm isn’t a JavaScript replacement for typical DOM manipulation and UI logic — the interop overhead between JS and Wasm makes it a poor fit for chatty, small operations. It shines specifically for self-contained, computation-heavy work.