The blur feels slow. The temptation is to change compiler settings or rewrite the function straight away. The right first step: measure where your program's time and bytes go, before you change anything. That is profiling.
Mrs. Sri did the same when breakfast at Mist Homestay kept running late. She didn't buy a new stove straight away. She held a stopwatch and noted the total time each morning, then one morning she followed the cook and noted how long each job took, then she opened the storeroom to see what took the most space. A jar without a label can only be written down as "jar number 0". The stopwatch is a benchmark, following the cook is a profiler, opening the storeroom is a binary size analysis, and the jar labels are the name section in the .wasm file.
Measure first, honestly
You know the rules from the lesson "Wasm vs JavaScript": performance.now(), many rounds, drop the first round (warm-up), take the median, and check the results are identical.
function measure(fn, rounds = 10, warmUp = true) {
const times = [];
for (let i = 0; i < rounds; i++) {
const start = performance.now();
fn();
times.push(performance.now() - start);
}
const used = (warmUp ? times.slice(1) : times).sort((a, b) => a - b);
return { first: times[0], median: used[Math.floor(used.length / 2)] };
}
- The first round also pays for compiling and warming up the engine, so it is kept apart.
- The median resists noise better than the average. A busy laptop makes one or two rounds spike.
- Measure the whole path: copying the photo in and out of Wasm memory (0.6 ms) is paid every time too.
Measured in Chromium 147 on a busy MacBook M1 Pro, median after warm-up (11 rounds, debug build 4 rounds):
| Job on the 1200 × 800 photo | JavaScript | Wasm (release) | Wasm (debug build) |
|---|---|---|---|
| Grayscale | 1.98 ms | 0.90 ms | 8.6 ms |
| Gaussian blur σ 2 | 155 ms | 49 ms | 646 ms |
The debug build (cargo build without --release, opt-level = 0) is 13 times slower than release, and 4 times slower than JavaScript. Never conclude "Wasm is slow" from a debug build.
Chrome DevTools: Performance and function names
A benchmark tells you how long. A profiler tells you where. To use it: open DevTools (F12), the Performance tab, click Record, run the blur, click Stop. In the Bottom-Up tab, sort by Self Time: the function that worked longest is at the top. Wasm functions show up there next to JavaScript functions.
The name you see depends on what the .wasm file contains. This is the output of the same profiler the Performance panel uses (Chromium 147, grayscale and blur 3 times):
photolab.wasm build |
Top row in the profiler | Self time |
|---|---|---|
strip = true (4,177 bytes) |
wasm-function[0] in wasm://wasm/8de44fbe |
177.6 ms |
| Cargo default, name section kept (4,761 bytes) | _RNvCsaWdBoUbM9cW_9photo_lab14blur_direction in wasm://wasm/photo_lab.wasm-c7e9edd6 |
177.4 ms |
- The name section is a custom section holding function names. Without it, the engine only knows their index:
wasm-function[0]. - Rust names are written in mangled form (
_RNv...), butphoto_labandblur_directionare readable inside. - The same profile also shows a
js-to-wasmrow (0.8 ms): the cost of crossing from JS into Wasm. - Error stack traces use the same names:
at photo_lab.wasm.blur_h (wasm://wasm/photo_lab.wasm-c7e9edd6:wasm-function[3]:0x3be). Without a name section:at wasm://wasm/8de44fbe:wasm-function[3]:0x3be.
To debug down to source lines you need DWARF (full debug info): debug = true in Cargo (photolab becomes 38,183 bytes) or -g in Emscripten, plus the Chrome extension C/C++ DevTools Support (DWARF). With it you can set breakpoints on C/C++ lines, see variable values, and function names in the Call Stack. Ship a build without debug info to users, and keep a build with names for profiling.
What is big in my binary
wasm-tools objdump (wasm-tools 1.261.0) splits the file by section, in bytes:
$ wasm-tools objdump photolab.wasm
types | 0xa - 0x26 | 28 bytes | 4 count
functions | 0x28 - 0x34 | 12 bytes | 11 count
memories | 0x36 - 0x39 | 3 bytes | 1 count
exports | 0x3b - 0xa5 | 106 bytes | 8 count
code | 0xa8 - 0x1051 | 4009 bytes | 11 count
custom "name" | 0x1059 - 0x11aa | 337 bytes | 1 count
custom "producers" | 0x11b6 - 0x11f9 | 67 bytes | 1 count
custom "target_features" | 0x120c - 0x1299 | 141 bytes | 1 count
Then wasm-objdump -x -j Code photolab.wasm (wabt 1.0.42) gives the size of every function, with names from the name section: resize_bilinear 930 bytes, grayscale_simd 861, resize_area 815, blur_direction 757, resize_nearest 291, grayscale 272.
In bigger modules, what takes the space is often not your code. The photolab component in the next lesson uses Vec, String and format! from std. Its core module is 21,003 bytes, and 5,204 of them are a single function: dlmalloc::malloc, std's default allocator. Rust's formatting machinery (core::fmt) comes along too, 17 functions totalling 4,345 bytes, because of format! and panic messages. The dedicated tool for this, twiggy (cargo install twiggy, then twiggy top and twiggy dominators), still installs (version 0.8.0), but its repo has been archived since 14 March 2026. wasm-objdump and wasm-tools are still maintained.
Size versus speed: opt-level, LTO, wasm-opt
[profile.release]
opt-level = 3 # 3 = fastest; "s" and "z" = focus on size
lto = true # optimise across crates at link time
codegen-units = 1 # one unit: more thorough optimisation, slower compile
panic = "abort" # no unwinding machinery
strip = true # drop the name section and debug info
After that, wasm-opt (from Binaryen, version 133) optimises the .wasm file itself. -O1 to -O4 focus on speed (higher takes longer to run), -Os and -Oz focus on size:
wasm-opt -O3 --enable-simd --enable-bulk-memory --enable-sign-ext \
--enable-nontrapping-float-to-int --enable-mutable-globals \
--enable-multivalue --enable-reference-types \
photolab.wasm -o photolab.opt.wasm
Before you look at the table, guess: opt-level = "z" takes the file from 4,177 down to 2,637 bytes. Does the σ 2 blur get faster, stay the same, or get slower?
All these builds come from the same Rust code, produce byte-identical results, and were measured in turn (median, Chromium 147 and WebKit 26.4):
| Build | Size | Blur σ 2 Chromium | WebKit |
|---|---|---|---|
opt-level = 3, LTO, codegen-units = 1, strip |
4,177 bytes | 49 ms | 50 ms |
the same + wasm-opt -O3 |
3,683 bytes | 49 ms | 49 ms |
opt-level = "s" |
2,504 bytes | 95 ms | 173 ms |
opt-level = "z" |
2,637 bytes | 131 ms | 152 ms |
"z" + wasm-opt -Oz |
2,284 bytes | 126 ms | |
no LTO, codegen-units = 16 |
7,453 bytes | 47 ms | 48 ms |
debug build + wasm-opt -O3 |
15,548 bytes | 711 ms |
The "z" build turns out slower: 49 ms becomes 131 ms, almost 3 times. The profiler shows why: in the "z" build, Ord::clamp shows up on its own with 37 ms of self time. To save bytes, "z" does not inline that small function and turns off loop vectorisation, so every blur tap makes a function call. Grayscale stays at 0.9 ms in every release build.
- Release, wasm-opt, strip: the three most effective steps to shrink a
.wasm. Here wasm-opt trims 12% without changing speed, because LLVM had already optimised it. - wasm-opt cannot rescue a debug build: still 711 ms. Optimisation the compiler skipped can't be added back afterwards.
wasm-pack buildruns wasm-opt automatically.- Gzip or brotli on the server shrink the download, but the browser still compiles the whole file. That is covered in the lesson "Wasm in Production".
💡 When JavaScript is enough
- Grayscale: JS 1.98 ms, Wasm 0.90 ms plus 0.6 ms of copying. That saves about 0.5 ms per photo, once per upload. Not worth a Rust toolchain if that is all.
- The budgets: 16.7 ms per frame on a 60 Hz screen, and 50 ms before a task counts as a long task that makes the page feel stuck.
- Blur from 155 ms down to 49 ms: now it pays off, even more so in a Worker.
- Profile first. If the slow part turns out to be the DOM, the network or rendering, Wasm does not help.
⚠️ Numbers that mislead and optimisations that go the wrong way
- Measuring a debug build. Blur 646 ms against 49 ms. Always
--release. - Using
opt-level = "z"without measuring. It saves 1.5 KB and makes the blur almost 3 times slower. - Profiling without a name section. All you see is
wasm-function[0]. Make a profiling build with names. - wasm-opt without feature flags. A module that uses SIMD is rejected:
[wasm-validator error in function 6] unexpected false: SIMD operations require SIMD [--enable-simd]. - Forgetting the crossing and copying costs. Measure from JS making the call until the result is back in JS.
TL;DR: Measure before optimising: performance.now(), many rounds, drop the warm-up, take the median, check the results match, and always use a release build (debug build: blur 646 ms against 49 ms). The Performance tab in Chrome DevTools shows Wasm functions by their real names when the file has a name section or DWARF; without them you only get wasm-function[0]. DWARF plus the "C/C++ DevTools Support (DWARF)" extension gives you breakpoints in your source code. wasm-tools objdump and wasm-objdump -x show the biggest sections and functions; twiggy still installs but is archived. To shrink: release, wasm-opt, strip (4,761 down to 3,683 bytes without losing speed). opt-level = "z" is smaller (2,637 bytes) but makes the blur 131 ms because small functions are not inlined. If JavaScript is already under budget (16.7 ms per frame, 50 ms per task), you don't need Wasm yet.
⚡ Measure first, honestly
Write:
function measure(fn, rounds = 10, warmUp = true) {
const times = [];
for (let i = 0; i < rounds; i++) {
const start = performance.now();
fn();
times.push(performance.now() - start);
}
const used = (warmUp ? times.slice(1) : times).sort((a, b) => a - b);
return { first: times[0], median: used[Math.floor(used.length / 2)] };
}
Meaning: Keep the first round apart, take the median, and always measure a release build. A debug build makes the σ 2 blur take 646 ms, against 49 ms in release.
⚡ The profiler shows where
Meaning: In the DevTools Performance tab, record the blur, then sort Bottom-Up by Self Time. When the .wasm file has a name section, Wasm functions show up under their real names. Without it, all you see is wasm-function[0].
⚡ Smaller is not always faster
Guess first: opt-level = "z" takes the file from 4,177 down to 2,637 bytes. Does the σ 2 blur get faster, stay the same, or get slower?
Slower: 49 ms becomes 131 ms. To save bytes, small functions like Ord::clamp are not inlined, so every blur tap makes a function call.
Meaning: Measure before you use a setting that focuses on size.
⚡ Shrinking it safely
Write:
[profile.release]
opt-level = 3 # 3 = fastest; "s" and "z" = focus on size
lto = true # optimise across crates at link time
codegen-units = 1 # one unit: more thorough optimisation, slower compile
panic = "abort" # no unwinding machinery
strip = true # drop the name section and debug info
Meaning: Release, wasm-opt and strip are the three most effective steps: here wasm-opt trims 12% without changing speed. Keep one build with names for profiling too.