WebAssembly Demo
This project demonstrates how to create WebAssembly modules using Rust and wasm-bindgen that can be used in web browsers.
Overview
The WASM module provides:
- Arithmetic functions:
add(),multiply(),divide(),factorial(),is_prime() - String functions:
greet(),to_uppercase(),reverse_string(),word_count(),is_palindrome() - Console logging:
log(),warn(),error() - DOM manipulation:
set_element_text(),get_element_text(),append_paragraph() - Classes:
Counter,Point,Calculator
Prerequisites
- Rust toolchain (rustc, cargo)
- wasm-pack:
cargo install wasm-pack - A simple HTTP server (e.g.,
python -m http.serverornpx serve)
Building
Install wasm-pack
Build for Web
This creates the pkg/ directory containing:
wasm_demo_bg.wasm- The WebAssembly binarywasm_demo.js- JavaScript bindingswasm_demo.d.ts- TypeScript definitions
Build for Node.js
Build for Bundlers (webpack, rollup, etc.)
Running
Using a Local Server
# Build the WASM module
# Copy the pkg directory to www
# (or symlink: mklink /D www\pkg pkg on Windows)
# Start a local server in the www directory
# Or: npx serve
# Or: npx http-server
# Open http://localhost:8080 in your browser
Using PowerShell (Windows)
# Build
wasm-pack build --target web
# Copy pkg to www
Copy-Item -Path pkg -Destination www\pkg -Recurse -Force
# Start server
cd www
python -m http.server 8080
Usage in JavaScript
Basic Functions
import init from './pkg/wasm_demo.js';
;
Using Classes
import init from './pkg/wasm_demo.js';
;
Console Logging
import init from './pkg/wasm_demo.js';
;
DOM Manipulation
import init from './pkg/wasm_demo.js';
;
Running Tests
Rust Unit Tests
WASM Tests in Browser
# Or: wasm-pack test --headless --chrome
Optimization
The release profile is already optimized for size:
[]
= "s" # Optimize for size
= true # Enable link-time optimization
To further reduce size:
# Install wasm-opt
# Build with wasm-pack
# Optimize the WASM binary
Performance
WebAssembly provides near-native performance. Here's a benchmark example:
import init from './pkg/wasm_demo.js';
;
Project Structure
wasm_demo/
├── Cargo.toml # Rust configuration
├── src/
│ └── lib.rs # Rust source code
├── pkg/ # Generated by wasm-pack
│ ├── wasm_demo_bg.wasm
│ ├── wasm_demo.js
│ └── wasm_demo.d.ts
└── www/ # Web demo
└── index.html # Interactive demo page
Using in Production
With a Bundler (webpack, vite, etc.)
# Build for bundler
# In your JavaScript
;
) {
);
));
}
Publishing to npm
# Build the package
# Login to npm
# Publish
Common Issues
Module Not Found
Make sure to:
- Build the WASM module first:
wasm-pack build --target web - Serve the files via HTTP (not
file://) - Check that the import path is correct
CORS Errors
Always serve WASM files via HTTP server, not directly from the filesystem.
Memory Issues
For large data processing:
- Pass data by reference when possible
- Use typed arrays for efficient data transfer
- Consider streaming APIs for large datasets
TypeScript Support
The generated pkg/wasm_demo.d.ts provides full TypeScript support:
import init, { Counter, Point } from './pkg/wasm_demo';
async function run() {
await init();
const counter: Counter = new Counter();
counter.increment();
const p1: Point = new Point(0, 0);
const p2: Point = new Point(3, 4);
const distance: number = p1.distance_to(p2);
}
run();
Browser Compatibility
WebAssembly is supported in:
- Chrome/Edge 57+
- Firefox 52+
- Safari 11+
- Opera 44+
For older browsers, consider using a polyfill or fallback to JavaScript.
Learning Resources
License
This example is provided for educational purposes.