WebAssembly Browser Support and Detection
How to detect WebAssembly support, load WASM modules, and understand browser compatibility. Covers streaming compilation and feature detection.
Security
Detailed Explanation
WebAssembly Detection
WebAssembly (Wasm) enables near-native performance for computation-heavy tasks in the browser. It is a binary instruction format that runs in a sandboxed virtual machine alongside JavaScript.
Detection
const hasWasm = typeof WebAssembly !== 'undefined'
&& typeof WebAssembly.instantiate === 'function';
Streaming Compilation
Modern browsers support streaming compilation, which compiles Wasm modules while they are still downloading:
async function loadWasm(url) {
if (WebAssembly.instantiateStreaming) {
const { instance } = await WebAssembly.instantiateStreaming(
fetch(url)
);
return instance;
}
// Fallback for older browsers
const response = await fetch(url);
const bytes = await response.arrayBuffer();
const { instance } = await WebAssembly.instantiate(bytes);
return instance;
}
Feature Tiers
| Feature | Detection |
|---|---|
| Basic Wasm | typeof WebAssembly !== 'undefined' |
| Streaming | WebAssembly.instantiateStreaming |
| Threads | WebAssembly.Memory with shared: true |
| SIMD | Compile a SIMD test module |
| Exception handling | Compile a test module with try/catch |
Performance Considerations
- Wasm code typically runs at 80-95% of native speed
- First load includes compilation time; subsequent loads use cached compiled modules
- Wasm and JavaScript can call each other, but boundary crossing has overhead
- Large Wasm binaries benefit from streaming compilation and lazy loading
Use Case
Used for compute-intensive applications like image/video processing (Photoshop web, Figma), game engines (Unity, Unreal), cryptographic operations, scientific simulations, and porting existing C/C++/Rust codebases to the web.