# I built a background remover that runs 100% in the browser. Here's how I made it 7x faster

> Source: <https://dev.to/manish_joshi_5453f51f4e5b/i-built-a-background-remover-that-runs-100-in-the-browser-heres-how-i-made-it-7x-faster-14lp>
> Published: 2026-11-21 06:04:01+00:00

**Recommendation:** Cap ONNX Runtime's thread count before the library loads, and serve model chunks with immutable Cache-Control headers so returning visitors load from disk cache.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

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](https://removedefundo.com/en/), 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.**

Everything happens client-side. The server only serves static files.

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.

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.

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:

``` js
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**.

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:

`preload()` with `JSON.stringify(config)`).
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.

`isnet_quint8`, 44 MB).
|  | 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.

The heavy lifting is done by [IMG.LY's background-removal-js](https://github.com/imgly/background-removal-js) (AGPL-3.0) and [ONNX Runtime Web](https://onnxruntime.ai/). If you build something similar, check their licenses.

You can try it here: **[removedefundo.com/en](https://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.*
