Skip to content

razinahmed/rust-wasm-toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust WebAssembly Toolkit Banner

Rust WebAssembly Toolkit

Starter kit for compiling high-performance Rust into WebAssembly — wasm-pack, wasm-bindgen, JavaScript interop, benchmarks, and production-ready build pipeline.

Rust WebAssembly wasm-pack JavaScript License: MIT

Getting Started · Features · Tech Stack · Benchmarks · Contributing


✨ Features

Feature Description
📦 wasm-pack Build Pipeline One-command compilation from Rust to optimized .wasm binaries with npm package output
🔌 wasm-bindgen JS Interop Seamless two-way data passing between Rust and JavaScript with auto-generated bindings
🛡️ Memory-safe Computations Leverage Rust's ownership model for zero-cost abstractions in browser workloads
🌐 Browser & Node.js Support Target both browser ESM bundles and Node.js CommonJS modules from a single codebase
📊 Performance Benchmarks Built-in benchmark suite comparing WASM vs. native JS for common compute tasks
🌲 Tree-shaking Dead code elimination ensures minimal bundle size for production deploys
🗺️ Source Maps Debug-friendly source maps mapping .wasm back to original Rust source
⚙️ CI/CD GitHub Actions pipeline for build, test, benchmark, and npm publish

🛠️ Tech Stack

Technology Purpose
Rust Systems programming language
WebAssembly Binary instruction format for the web
wasm-pack Build, test, and publish WASM packages
wasm-bindgen Rust/JS interop layer
JavaScript Host language and integration tests
webpack Module bundler with WASM support

📦 Installation

Prerequisites

  • Rust >= 1.75 — Install via rustup
  • wasm-packcargo install wasm-pack
  • Node.js >= 18.x (for JS integration and benchmarks)
  • npm >= 9.x

Getting Started

# Clone the repository
git clone https://github.com/razinahmed/rust-wasm-toolkit.git

# Navigate to the project
cd rust-wasm-toolkit

# Build the WASM package
wasm-pack build --target web

# Install JS dependencies for the demo app
cd www && npm install

# Start the development server
npm run start

Build Targets

# Browser (ESM)
wasm-pack build --target web --release

# Node.js (CommonJS)
wasm-pack build --target nodejs --release

# Bundler (webpack/rollup)
wasm-pack build --target bundler --release

📂 Project Structure

rust-wasm-toolkit/
├── src/
│   ├── lib.rs              # Main library entry point
│   ├── math.rs             # Math computation modules
│   ├── string_ops.rs       # String processing utilities
│   ├── crypto.rs           # Hashing and encoding
│   └── utils.rs            # Shared helpers and panic hook
├── www/
│   ├── index.html          # Demo application
│   ├── index.js            # JS integration layer
│   ├── webpack.config.js   # Bundler configuration
│   └── package.json
├── benches/
│   ├── wasm_bench.rs       # Rust-side benchmarks
│   └── js_bench.mjs        # JS comparison benchmarks
├── tests/
│   ├── web.rs              # WASM integration tests
│   └── node_tests.mjs      # Node.js integration tests
├── Cargo.toml
├── .github/
│   └── workflows/
│       └── ci.yml
└── README.md

⚡ Usage

Rust Side — Exposing Functions

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 temp = b;
                b = a + b;
                a = temp;
            }
            b
        }
    }
}

JavaScript Side — Consuming WASM

import init, { fibonacci } from './pkg/rust_wasm_toolkit.js';

async function main() {
  await init();
  const result = fibonacci(50);
  console.log(`Fibonacci(50) = ${result}`); // Computed in ~nanoseconds
}

main();

📊 Benchmarks

Operation JavaScript WASM (Rust) Speedup
Fibonacci(40) 820ms 12ms ~68x
SHA-256 (1MB) 145ms 18ms ~8x
Matrix Multiply (512x512) 1,200ms 85ms ~14x

Run benchmarks locally: npm run bench


🤝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch — git checkout -b feature/new-module
  3. Commit your changes — git commit -m "feat: add new computation module"
  4. Push to the branch — git push origin feature/new-module
  5. Open a Pull Request

Please ensure all tests pass with wasm-pack test --headless --firefox before submitting.


📜 License

This project is licensed under the MIT License. See the LICENSE file for details.


Built with ❤️ by Razin Ahmed

Rust WebAssembly WASM wasm-pack High Performance Browser Rust to JS

About

Rust + WebAssembly toolkit — high-performance browser modules with wasm-pack and JS interop

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors