WebAssembly (WASM) in Production: Edge Functions & Performance Guide
WebAssembly brings near-native performance to edge functions, enabling CPU-intensive operations โ image processing, cryptography, data parsing โ that would be too slow in JavaScript. This guide covers WASM runtimes at the edge, building with Rust, performance benchmarks, and production patterns.
Edge WASM Runtimes Comparison
| Platform | WASM Support | Max Module Size | Languages | WASI Support |
|---|---|---|---|---|
| Cloudflare Workers | Native (WebAssembly.instantiate) | 2MB per module | Rust, C/C++, Go, AssemblyScript | Partial (WASI preview 1) |
| Fastly Compute | Primary runtime | 100MB | Rust, Go, JavaScript, C, Python | Full WASI 0.2 |
| Node.js (v8+) | WebAssembly API | No hard limit | Any WASM-compilable language | Via @wasmer/wasi |
| WasmEdge | Dedicated runtime | No hard limit | Rust, C, JavaScript, Python | Full WASI support |
Building WASM from Rust with wasm-pack
# 1. Install wasm-pack
cargo install wasm-pack
# 2. Create Rust library (Cargo.toml)
[package]
name = "edge-crypto"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
sha2 = "0.10"
hex = "0.4"
# 3. Rust WASM code (src/lib.rs)
use wasm_bindgen::prelude::*;
use sha2::
{Sha256, Digest};
#[wasm_bindgen]
pub fn hash_sha256(input: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(input.as_bytes());
hex::encode(hasher.finalize())
}
#[wasm_bindgen]
pub fn count_tokens_approx(text: &str) -> u32 {
// Approximate: 1 token โ 4 chars
(text.len() as u32 + 3) / 4
}
# 4. Build for Cloudflare Workers
wasm-pack build --target bundler --release
# 5. Optimize with wasm-opt (binaryen)
wasm-opt -O3 -o pkg/edge_crypto_bg_opt.wasm pkg/edge_crypto_bg.wasm
# Typically reduces size by 15-30% Using WASM in a Cloudflare Worker
// worker.ts โ import and use WASM module
import init, { hash_sha256, count_tokens_approx } from './pkg/edge_crypto.js';
import wasmModule from './pkg/edge_crypto_bg.wasm';
// Initialize WASM once at module scope (not inside handler)
// This runs during Worker initialization, not per-request
const wasmReady = init(wasmModule);
export default {
async fetch(request: Request): Promise<Response> {
await wasmReady; // Ensure WASM is initialized
const url = new URL(request.url);
const text = url.searchParams.get('text') ?? '';
// Call WASM function (near-native speed)
const hash = hash_sha256(text);
const tokenCount = count_tokens_approx(text);
return Response.json({
hash,
tokenCount,
textLength: text.length,
});
}
};
// wrangler.toml โ enable WASM
// [build]
// command = "npm run build"
// [build.upload]
// format = "modules" WASM vs JavaScript Performance Benchmarks
| Task | JavaScript | Rust WASM | WASM Speedup |
|---|---|---|---|
| SHA-256 hash (1MB) | ~8ms | ~1.5ms | 5.3x faster |
| JSON parse + transform | ~2ms | ~0.8ms | 2.5x faster |
| Image resize (100KB) | ~45ms | ~9ms | 5x faster |
| Markdown parse | ~3ms | ~0.9ms | 3.3x faster |
| HTTP request (I/O bound) | ~50ms | ~50ms | No difference |
Related Products
on GumroadDevToolkit Starter Kit
A ready-to-deploy developer tools website with 12 built-in tools. Astro + Preact + Tailwind. Deploy to Cloudflare Pages for free. Built-in SEO, dark mode, and ad-ready layout.
Buy on Gumroad โDeveloper Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad โAI Prompt Engineering Toolkit
58 structured prompt templates for Claude, ChatGPT, and Gemini โ organized by use case. Code Review, Debugging, Documentation, Architecture, and 10 model-specific tips. Stop writing prompts from scratch.
Buy on Gumroad โYou Might Also Need
WebAssembly Binary Inspector
Inspect WebAssembly (WAT) text format or hex binary. Parse sections (type, func, memory, export, import), count instructions, and estimate binary size.
Open free tool โ
WASM Module Explorer
Inspect WebAssembly modules by pasting WAT source or uploading a .wasm binary. View exports, imports, functions, memory, globals, and table declarations. Runs entirely in your browser.
Open free tool โ
Web Performance Checklist
Interactive web performance checklist covering images, fonts, JavaScript, CSS, caching, and server. Check off items to track your score out of 100 and export as Markdown.
Open free tool โ
More Free Tools
Related Articles
WebAssembly in Production: Edge Functions & Runtime Performance 2026
How to run WebAssembly in production edge functions in 2026: Cloudflare Workers, Fastly Compute, building with Rust and wasm-pack, performance benchmarks, WASM Component Model, and WASI.
Rust and WebAssembly in 2026: From Zero to Production
Learn how to compile Rust code to WebAssembly for blazing-fast web applications. A complete guide covering wasm-pack, wasm-bindgen, web-sys, JavaScript integration, performance optimization, and real-world case studies from Figma, Google Earth, and more.
Rust for Web Development 2026: Actix, Axum & WASM Integration Guide
Complete guide to Rust web development in 2026. Compare Actix-web vs Axum, build REST APIs with real code examples, integrate WebAssembly (WASM) with JavaScript, and understand when Rust web development makes sense for your project.
WebAssembly in Production 2026: Performance, Runtimes, and Use Cases
Complete guide to WebAssembly in production: WASM runtimes (Wasmtime, WasmEdge), Rust+wasm-pack, AssemblyScript toolchains, performance benchmarks, and real-world use cases.
WebAssembly 2026: From Browser to Cloud and Edge
WebAssembly has grown far beyond its browser origins. In 2026, WASM powers Figma's rendering engine, Fastly's edge compute, and Cloudflare Workers. Here's what you need to know to start using it.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- โก AI Code Review, Doc Generator & SQL Builder
- โก All premium templates & early access
- โก Member discounts on Gumroad products
$9/mo after trial ยท Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products โ grab them all at once and save big.
MIT licensed ยท Instant download ยท No subscription
See what's included โ Browse all deals โFrequently Asked Questions
What is WebAssembly (WASM)?▼
WebAssembly is a binary instruction format that serves as a portable compilation target for languages like C, C++, Rust, and Go. It runs at near-native speed in browsers via a sandboxed VM, and in server-side environments like Cloudflare Workers, Fastly Compute, Node.js, and Deno. WASM provides a deterministic, sandboxed execution environment with predictable performance characteristics regardless of the host platform.
Why use WASM in edge functions instead of JavaScript?▼
JavaScript is fast for general web workloads, but WASM excels at CPU-intensive operations: image processing, cryptographic operations, data parsing, compression/decompression, and numerical computation. In benchmarks, WASM typically outperforms equivalent JavaScript by 2-5x for CPU-bound tasks. WASM also enables using Rust, Go, or C libraries in edge environments that otherwise only support JavaScript.
How do I compile Rust to WASM for Cloudflare Workers?▼
Use wasm-pack to compile Rust to WASM with JavaScript bindings. Install wasm-pack, add wasm-bindgen to your Cargo.toml, annotate public functions with #[wasm_bindgen], then run wasm-pack build --target bundler. The output includes a .wasm file and JavaScript glue code. For Cloudflare Workers specifically, use the workers-rs crate for a more integrated experience.
What are the size limits for WASM modules in edge functions?▼
Cloudflare Workers: WASM modules count against the 1MB script size limit on Free plan, 10MB on Paid plan. The WASM module itself must be under 2MB per module in Workers. Fastly Compute: WASM binary can be up to 100MB. For size optimization, use wasm-opt (part of binaryen toolchain) with -O3 or -Os flags to strip unused code and minimize the binary. Typical Rust WASM modules compile to 100KB-2MB.
What are the limitations of WASM in edge environments?▼
WASM in edge functions cannot: access the DOM (server-side), make TCP connections (edge restrictions), use threads (Workers are single-threaded), access the file system, or use WASI system calls that require OS interaction. WASM can use: fetch API (HTTP), Web Crypto API, Workers KV/R2/D1 bindings (via JavaScript imports), and synchronous computation. The Component Model (WASI 0.2) is improving cross-language interoperability.
What is the WASM Component Model and why does it matter?▼
The WASM Component Model (WASI 0.2) is a standard for composing WASM modules from different languages. It defines Interface Types (WIT) that let a Rust WASM module expose typed interfaces consumable from JavaScript or Go without manual binding code. This is significant for edge function ecosystems because it allows polyglot composition โ a Python-compiled WASM module can call a Rust-compiled WASM module without JavaScript glue code.