WASM Optimization

Optimize WebAssembly bundle for production.

Build Command

cargo build --target wasm32-unknown-unknown --release
wasm-opt -O3 -o output_opt.wasm output.wasm

Optimization Flags

# Cargo.toml
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"

[profile.release.package."*"]
opt-level = "z"  # Size optimization for deps

Size Reduction

StepSize
Debug build~5MB
Release build~800KB
wasm-opt -O3~500KB
gzip~150KB

wasm-opt Levels

LevelFocusUse
-O1Fast compileDevelopment
-O2BalancedCI
-O3Max speedProduction
-OzMin sizeMobile

Code Splitting

// Lazy load large features
#[cfg(feature = "charts")]
mod charts;

Remove Dead Code

[dependencies]
serde = { version = "1", default-features = false }

Performance Tips

TipImpact
Use #[inline] wiselyReduces call overhead
Avoid Box<dyn Trait>Static dispatch faster
Minimize allocationsReuse buffers
Use &str over StringZero-copy

Measuring Size

# Show section sizes
wasm-objdump -h output.wasm

# Find large functions
wasm-objdump -d output.wasm | grep "func" | sort -k2 -n -r | head

Bundle Analysis

# Size breakdown
twiggy top output.wasm

# Dependency graph
twiggy dominators output.wasm

Verified Test

#[test]
fn test_optimization_config() {
    // Verify release profile exists
    #[cfg(debug_assertions)]
    let is_release = false;
    #[cfg(not(debug_assertions))]
    let is_release = true;

    // In release mode, optimizations are active
    if is_release {
        assert!(true);
    }
}