I built a background remover that runs 100% in the browser. Here's how I made it 7x faster
Most "free" background removers upload your photo to a server, run a model on a GPU, and then ask you to pay for the full-resolution download. I wanted the opposite: the photo never leaves your device, the download is fu
Most "free" background removers upload your photo to a server, run a model on a GPU, and then ask you to pay for the full-resolution download. I wanted the opposite: the photo never leaves your device, the download is full resolution, and there is no account.
So I built Remove de Fundo, a background remover where the AI model runs entirely in the browser. The first version worked, but on real phones it was slow enough that people left before seeing a result. This post is about what made it slow and what fixed it.
End result on the same laptop and the same photo: 43.8 s → 6.4 s from click to cutout.
The stack
- Astro for the site (static pages, deployed to Cloudflare Workers)
- @imgly/background-removal with the IS-Net fp16 model (~88 MB)
- ONNX Runtime Web, which runs the model with WebGPU on desktop and WebAssembly (CPU) on phones
- A small post-processing step in canvas to clean up semi-transparent "haze" in the mask
Everything happens client-side. The server only serves static files.
Problem 1: the model came from a CDN that didn't cache
The first visit downloads ~100 MB (model + ONNX runtime). That is unavoidable for an in-browser model, but I noticed something worse: the CDN serving the model sent no Cache-Control header, and the edge marked it cf-cache-status: DYNAMIC. So returning visitors were often downloading the model again, and nothing was cached near the user.
Fix: self-host the model files. A build script downloads the model chunks once and puts them in public/ai/<version>/, and the library is pointed there with publicPath. Since the chunk file names are content hashes, they can be cached forever:
/ai/*
Cache-Control: public, max-age=31536000, immutable
Now a returning visitor loads the model from disk cache: zero network.
One gotcha: the chunk files have no extension, and both the dev server and the host treated extensionless paths as pages (404). Saving them as <hash>.bin and rewriting the names in resources.json fixed it.
Problem 2: the CPU path used one core out of eight
Phones always use the WebAssembly path, so CPU speed matters most. ONNX Runtime Web can use multiple threads, but only if the page is cross-origin isolated, because threads need SharedArrayBuffer.
My page wasn't isolated:
crossOriginIsolated // false
Fix: two headers.
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: credentialless
I used credentialless instead of require-corp so Google Fonts and analytics keep working without CORP headers. Browsers that don't support it simply ignore it, so nothing breaks there.
Result (CPU, model already loaded): 22.7 s → 7.9 s per photo. Almost 3x, from two lines of config.
Problem 3: the bug that made PNG encoding 13x slower
After enabling isolation, inference got much faster but the total barely improved. I logged a timestamp for each stage and found this:
| Stage | Time |
|---|---|
| Inference | 1.5 s |
| PNG encode | 5.5 s |
| Edge refinement | 4.4 s |
Encoding a 1200 × 800 PNG should take a fraction of a second. I measured canvas.convertToBlob() on an idle page:
| Page | PNG encode |
|---|---|
| Not isolated | 208 ms |
| Isolated, before loading ONNX | 217 ms |
| Isolated, after running ONNX | 2,648 ms |
So isolation itself was fine. The problem: the library sets the WASM thread count to navigator.hardwareConcurrency (all cores), and ONNX's worker threads keep spin-waiting after inference. They occupy every core, and the browser's own image-encoding threads starve.
Fix: cap the thread count before the library loads. I used ONNX Runtime's own default (half the cores, at most 4) on CPU, and 1 thread on the GPU path, where the GPU does the work:
function limitOnnxThreads(device: 'gpu' | 'cpu') {
const cores = navigator.hardwareConcurrency || 4;
const threads = device === 'gpu' ? 1 : Math.max(1, Math.min(4, Math.ceil(cores / 2)));
Object.defineProperty(navigator, 'hardwareConcurrency', {
configurable: true,
get: () => threads,
});
}
Post-processing went from ~10 s back to 0.2 s.
Problem 4: the first photo on WebGPU paid for shader compilation
On the WebGPU path, the second photo took 2 s but the first took ~17 s on every page load. The session creation and, mostly, the first inference compile the GPU shaders.
Fix: warm up on intent. When the user hovers the upload area (desktop) or taps it (mobile), I:
- Prefetch the model files into the HTTP cache (low priority).
- Call the library's
preload()with exactly the same config as the real run (the library memoizes the session byJSON.stringify(config)). - On GPU only, run one throwaway inference on a tiny 32×32 image. The model input has a fixed size, so this compiles everything the real photo needs.
While the person is still picking a photo from their gallery, all of that is already happening. I skip the warm inference on CPU: on a phone it would take as long as a real cutout and the user's photo would wait behind it. I also skip prefetching on saveData or 2G connections, and on mobile only a tap on the upload box triggers it, so scrolling past doesn't burn 100 MB of someone's data.
Things I tried that didn't work
-
The smaller quantized model (
isnet_quint8, 44 MB). Half the download, but it left patches of background all over a portrait. Not worth it. - Relying on HTTP heuristic caching for a third-party CDN. In practice, returning visitors still re-downloaded.
UX fixes that mattered as much as speed
- The upload box was below the fold on a 730 px laptop screen. People saw a headline and left. Tightening the hero put the button in the first screen on laptops and phones.
- Show the user's own photo with a scanning line while processing, instead of a generic spinner. It makes the wait feel much shorter.
- On mobile, the label "Drag your photo here" makes no sense. It now says "Choose a photo".
Results
| Before | After | |
|---|---|---|
| Click → result (laptop, first run) | 43.8 s | 6.4 s |
| CPU inference per photo | 22.7 s | 7.9 s |
| PNG encode + edge cleanup | ~10 s (isolated, before fix) | 0.2 s |
| Returning visitor model download | often ~100 MB | 0 (disk cache) |
Since adding analytics, about 98% of removals succeed and more than half of the people who upload a photo download the result.
Credits
The heavy lifting is done by IMG.LY's background-removal-js (AGPL-3.0) and ONNX Runtime Web. If you build something similar, check their licenses.
You can try it here: removedefundo.com/en. It's free, with no sign-up, and works in English, Spanish and Portuguese. I'd love feedback, especially from people on older phones.
This post was written with AI assistance. The numbers come from real measurements on the live site.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.