Node.js WebAssembly

Node.js Tutorial

πŸš€ Node.js WebAssembly (WASM) Tutorial

WebAssembly (WASM) allows you to run high-performance, low-level code (C, C++, Rust, Go, etc.) inside Node.js with near-native speed.

Node.js supports WebAssembly natively β€” no browser required.


🧠 What is WASM ?

WebAssembly (WASM) is:

  • A low-level binary format

  • Fast and optimized for performance

  • Supported by Node.js and all modern browsers

  • Ideal for CPU-heavy tasks

Examples where WASM is useful:

  • Image/Video processing

  • Crypto operations

  • Data compression

  • Game engines

  • Machine learning

  • Big mathematical calculations


πŸ”₯ Why Use WebAssembly in Node.js?

βœ” C/C++/Rust code running inside Node
βœ” Up to 10x–100x faster
βœ” Share code between Browser + Server
βœ” Great for microservices needing performance
βœ” Works with Node.js modules


πŸ›  1. Running a Simple WebAssembly File in Node.js

Let’s create a very simple WASM module that adds two numbers.


Step 1 β€” Create a WebAssembly Text File (.wat)

Create a file:
add.wat


Step 2 β€” Convert .wat β†’ .wasm

Install wabt tools:

npm install -g wabt

Compile:

wat2wasm add.wat -o add.wasm

Step 3 β€” Load WASM in Node.js

Create index.js:


 

Run:

node index.js

Output:

Result: 30

πŸŽ‰ You just ran WebAssembly inside Node.js!


πŸ›  2. Using Rust with WebAssembly in Node.js

Rust is the best language for producing WebAssembly.

Install WASM tools:

cargo install wasm-pack

Create project:

cargo new wasm_demo --lib
cd wasm_demo

Edit src/lib.rs

#[no_mangle]
pub extern "C" fn multiply(a: i32, b: i32) -> i32 {
a * b
}

Build WASM:

wasm-pack build --target nodejs

You’ll get a /pkg folder with WASM + JS bindings.

Use in Node.js:

Create test.js:


 

Run:

56

πŸ›  3. Using C/C++ with WebAssembly (Emscripten)

Install Emscripten:

emsdk install latest
emsdk activate latest

Create add.c


Compile to WASM:

emcc add.c -Os -s WASM=1 -s EXPORTED_FUNCTIONS="['_add']" \
-o add.js

Node.js Example:


 


🧩 Calling JavaScript from WASM and vice-versa

You can pass:

  • Numbers

  • Arrays

  • Memory buffers

  • Strings

Example JS β†’ WASM:

wasm.instance.exports.process_value(123);

Example WASM β†’ JS:

#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}

βš™οΈ 4. Node.js + WebAssembly Performance Use Cases


πŸš€ High-Performance Microservices

Example tasks ideal for WASM:

  • Encryption (SHA-256, AES)

  • Image resizing

  • PDF processing

  • Data compression (gzip, brotli)

  • Video transcoding

  • AI model inference


🧱 5. WASI in Node.js

Node.js supports WASI, allowing WASM modules to access:

  • File system

  • OS

  • Network

  • Environment variables

Install WASI:

npm install @wasmer/wasi
npm install @wasmer/wasmfs

Example:



πŸ“¦ 6. Loading WebAssembly Dynamically


 


πŸ“˜ 7. Real-World Projects

Project Purpose
FFmpeg WASM Video processing
Squoosh Image compression
SQLite WASM Database in WASM
Blazor .NET in the browser
TensorFlow WASM Machine Learning

πŸŽ‰ Conclusion

Node.js + WebAssembly gives you:

  • ⚑ Near-native performance

  • 🧱 Ability to run C, C++, Rust in Node.js

  • πŸ” Secure sandbox execution

  • 🌐 Same WASM runs in Browser + Node

  • πŸ›  Perfect for heavy computations

You may also like...