This project demonstrates how to use WebAssembly in a web app and compares the performance of JavaScript to Wasm written in Rust
Create a Rust project. Follow the guide here to install Rust locally
cargo new wasm-fib-rust
Install wasm-pack
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
Add these to the Cargo.toml
file
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.84"
Rename main.rs
to lib.rs
and add the code below
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn wasmfib(n: i32) -> i32 {
if n < 1 {
return 1;
};
return wasmfib(n - 1) + wasmfib(n - 2);
}
Build the project with
wasm-pack build --target web
Clone the starter repo
cd ../
git clone https://github.com/megaconfidence/wasm-fib-calculator.git
Copy the pkg/
directory into the project root
cp -r wasm-fib-rust/pkg wasm-fib-calculator/pkg
Startup the project using a static file server
cd wasm-fib-calculator/
python3 -m http.server 8000
Visit the project on http://localhost:8000/