A browser benchmark can produce an impressive number while measuring only the easiest part of the experience. Timing one inference call can hide runtime loading, model transfer, compilation, initialization, output readback, and the work the surrounding interface must still perform.
For a feature used once during a form submission, those costs may matter more than its tenth inference. For a continuously running camera feature, repeated inference may dominate. The benchmark needs to match the interaction.
This walkthrough builds a small LiteRT.js harness that can request Wasm or WebGPU execution for the same model. It records the phases separately, preserves raw samples, and keeps backend configuration distinct from user-perceived speed. It does not assume that requesting the GPU means every operation ran there, or that a result from one machine predicts every device.
Fix the workload before changing the accelerator
The companion pins @litertjs/core to 2.6.0 and downloads Google’s MobileNetV2 demo model. The downloaded file is 14,020,256 bytes. Its SHA-256 is recorded in the supplied model manifest, so someone repeating the experiment can verify that the weights have not changed.
The harness uses one deterministic floating-point tensor. That makes the input identical across accelerator runs and avoids including image decoding in an inference-only measurement. It is deliberately not an image-recognition accuracy benchmark.
For a product evaluation, add a separate dataset of licensed images and a verified preprocessing pipeline. A model can return finite numbers for incorrectly normalized input. Output shape and absence of exceptions are useful smoke checks, not evidence that the classifier understands the images.
Keep initialization visible
LiteRT.js separates loading its runtime from loading and compiling a model. Fetch the model explicitly when you want its transfer time to remain visible.
await loadLiteRt("/wasm/");
const response = await fetch("/model.tflite");
if (!response.ok) throw new Error("Model download failed.");
const bytes = new Uint8Array(await response.arrayBuffer());
const model = await loadAndCompile(bytes, { accelerator });
The complete harness timestamps each phase and hashes the downloaded bytes outside the compilation interval. It reads the model’s input details rather than silently assuming a tensor layout. The example rejects models that do not have a single float32 input.
The supplied preparation command copies the runtime assets from the installed package. This keeps the JavaScript and Wasm versions aligned. Loading JavaScript from one release and Wasm from an unversioned CDN URL would introduce an avoidable variable.
Figure 1. Change the execution request while keeping the model and input fixed.
Include output readback in the timed operation
The example measures a result that JavaScript can actually inspect:
const start = performance.now();
const outputs = await model.run(input);
try {
const data = await outputs[0].data();
for (let i = 0; i < data.length; i++) {
if (!Number.isFinite(Number(data[i]))) {
throw new Error("Non-finite output.");
}
}
return performance.now() - start;
} finally {
outputs.forEach(output => output.delete());
}
Output transfer can matter when comparing an accelerator with CPU execution. If you exclude it, label the metric accordingly and explain why the application can avoid that transfer.
Release input tensors and the compiled model after the benchmark as well. Otherwise, repeated experiments can accumulate resources and change the environment you think you are holding constant.
Separate the first run from the warm runs
The harness records the first inference, performs three additional warmups, then retains ten measured samples. It reports the median without discarding the underlying observations.
| Recorded value | Question it helps answer |
|---|---|
| Runtime load time | How long did runtime initialization take? |
| Model fetch time and bytes | What did obtaining the weights cost? |
| Compilation time | What did preparing this backend cost? |
| First inference | What happens before the runtime is warm? |
| Ten warm samples | How variable is repeated execution here? |
| Output sample and length | Did both runs produce comparable numerical output? |
Ten samples are enough to inspect the harness, not to make a strong population-level tail-latency claim. Repeat whole sessions, alternate backend order, and report device conditions before drawing a performance conclusion.
Reload the page between accelerators. A reload resets the page’s runtime but does not necessarily clear the HTTP cache, operating-system caches, or driver caches. Describe the cache condition rather than calling every reload a cold start.
Check what WebGPU actually means in the run
The WebGPU branch first requests an adapter. If none is returned, it records an unsupported result instead of inventing a zero-duration measurement or quietly labeling a Wasm run as GPU execution.
LiteRT’s documentation notes that unsupported accelerated operations can fall back to CPU. Therefore, the requested accelerator is configuration evidence, not a complete execution profile. Capture available adapter information and use runtime diagnostics when making claims about where computation occurred.
A software adapter in a headless environment is also a different subject from a physical laptop GPU. Keep that distinction in the results table.
Run and interpret the experiment
From the companion’s lab directory, run npm ci, npm run prepare:model, and npm run build. Start npm run preview, open /benchmark.html, and run one accelerator per reload. Preserve the JSON output from each run with the browser and machine details.
The Wasm harness completed in Headless Chrome 153 on a virtualized Linux host: it accepted a [1, 3, 224, 224] input and returned 1,000 finite values through the first run, warmups, and ten measured runs. The raw JSON is included as an execution check. GPU execution was disabled in that environment, so these observations establish no WebGPU-versus-Wasm speed ranking. Hardware runs and an application-level responsiveness measurement remain necessary before recommending a backend for a real feature.
For the latter, measure an actual interaction while inference runs: typing into a field, opening a panel, or scrolling through results. A fast model invocation can coexist with a poor page experience if preprocessing or rendering blocks the main thread.
Which cost dominates your feature: obtaining the first useful answer, repeatedly processing frames, or keeping the surrounding interface responsive?

Top comments (1)
Great article, Raju! I really appreciate the distinction between faster inference and a genuinely faster user experience. It's easy to get excited about WebGPU benchmark numbers while overlooking initialization costs, CPU fallback, and UI responsiveness.
I'm curious, would you recommend dynamically choosing between Wasm and WebGPU based on the user's device capabilities and the type of workload? I'd love to hear your thoughts on this. 😊