{"slug": "i-built-a-background-remover-that-runs-100-in-the-browser-here-s-how-i-made-it", "title": "I built a background remover that runs 100% in the browser. Here's how I made it 7x faster", "summary": "A developer built Remove de Fundo, a browser-based background remover that runs its AI model entirely client-side, and cut click-to-cutout time from 43.8 seconds to 6.4 seconds on the same laptop and photo. The speedups came from self-hosting model files with immutable caching, enabling cross-origin isolation for SharedArrayBuffer threads, capping ONNX Runtime thread counts to stop spin-waiting from starving PNG encoding, and warming up WebGPU shaders on user intent.", "body_md": "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.\n\nSo 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.\n\n**End result on the same laptop and the same photo: 43.8 s → 6.4 s from click to cutout.**\n\nEverything happens client-side. The server only serves static files.\n\nThe 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.\n\n**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:\n\n```\n/ai/*\n  Cache-Control: public, max-age=31536000, immutable\n```\n\nNow a returning visitor loads the model from disk cache: zero network.\n\nOne 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.\n\nPhones 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`.\n\nMy page wasn't isolated:\n\n```\ncrossOriginIsolated // false\n```\n\n**Fix:** two headers.\n\n```\nCross-Origin-Opener-Policy: same-origin\nCross-Origin-Embedder-Policy: credentialless\n```\n\nI 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.\n\n**Result (CPU, model already loaded): 22.7 s → 7.9 s per photo.** Almost 3x, from two lines of config.\n\nAfter enabling isolation, inference got much faster but the total barely improved. I logged a timestamp for each stage and found this:\n\n| Stage | Time | \n|---|---|\n| Inference | 1.5 s | \n| PNG encode | **5.5 s** | \n| Edge refinement | **4.4 s** | \n\nEncoding a 1200 × 800 PNG should take a fraction of a second. I measured `canvas.convertToBlob()` on an idle page:\n\n| Page | PNG encode | \n|---|---|\n| Not isolated | 208 ms | \n| Isolated, before loading ONNX | 217 ms | \n| **Isolated, after running ONNX** | **2,648 ms** | \n\nSo 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.\n\n**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:\n\n``` js\nfunction limitOnnxThreads(device: 'gpu' | 'cpu') {\n  const cores = navigator.hardwareConcurrency || 4;\n  const threads = device === 'gpu' ? 1 : Math.max(1, Math.min(4, Math.ceil(cores / 2)));\n  Object.defineProperty(navigator, 'hardwareConcurrency', {\n    configurable: true,\n    get: () => threads,\n  });\n}\n```\n\nPost-processing went from ~10 s back to **0.2 s**.\n\nOn 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**.\n\n**Fix:** warm up on intent. When the user hovers the upload area (desktop) or taps it (mobile), I:\n\n`preload()` with `JSON.stringify(config)`).\nWhile 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.\n\n`isnet_quint8`, 44 MB).\n|  | Before | After | \n|---|---|---|\n| Click → result (laptop, first run) | 43.8 s | **6.4 s** | \n| CPU inference per photo | 22.7 s | **7.9 s** | \n| PNG encode + edge cleanup | ~10 s (isolated, before fix) | **0.2 s** | \n| Returning visitor model download | often ~100 MB | **0 (disk cache)** | \n\nSince adding analytics, about 98% of removals succeed and more than half of the people who upload a photo download the result.\n\nThe 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.\n\nYou 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.\n\n*This post was written with AI assistance. The numbers come from real measurements on the live site.*", "url": "https://wpnews.pro/news/i-built-a-background-remover-that-runs-100-in-the-browser-here-s-how-i-made-it", "canonical_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_at": "2026-11-21 06:04:01+00:00", "updated_at": "2026-11-21 06:12:05.446418+00:00", "lang": "en", "topics": ["ai-tools", "ai-infrastructure", "computer-vision", "developer-tools", "ai-products"], "entities": ["Remove de Fundo", "ONNX Runtime Web", "WebGPU", "WebAssembly", "SharedArrayBuffer"], "also_reported_by": [], "takeaway": "Capping ONNX Runtime's WASM threads to half the cores (max 4) on CPU and 1 on GPU cut post-processing from about 10 seconds to 0.2 seconds by stopping spin-waiting workers from starving the browser's PNG encoder.", "recommendation": {"advice": "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.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/i-built-a-background-remover-that-runs-100-in-the-browser-here-s-how-i-made-it", "markdown": "https://wpnews.pro/news/i-built-a-background-remover-that-runs-100-in-the-browser-here-s-how-i-made-it.md", "text": "https://wpnews.pro/news/i-built-a-background-remover-that-runs-100-in-the-browser-here-s-how-i-made-it.txt", "jsonld": "https://wpnews.pro/news/i-built-a-background-remover-that-runs-100-in-the-browser-here-s-how-i-made-it.jsonld"}}