<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manish joshi</title>
    <description>The latest articles on DEV Community by Manish joshi (@manish_joshi_5453f51f4e5b).</description>
    <link>https://dev.to/manish_joshi_5453f51f4e5b</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4256762%2F11a0c74a-fab4-4c15-9a15-5c26f6785a7a.png</url>
      <title>DEV Community: Manish joshi</title>
      <link>https://dev.to/manish_joshi_5453f51f4e5b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manish_joshi_5453f51f4e5b"/>
    <language>en</language>
    <item>
      <title>I built a background remover that runs 100% in the browser. Here's how I made it 7x faster</title>
      <dc:creator>Manish joshi</dc:creator>
      <pubDate>Sat, 21 Nov 2026 06:04:01 +0000</pubDate>
      <link>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</link>
      <guid>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</guid>
      <description>&lt;p&gt;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: &lt;strong&gt;the photo never leaves your device&lt;/strong&gt;, the download is full resolution, and there is no account.&lt;/p&gt;

&lt;p&gt;So I built &lt;a href="https://removedefundo.com/en/" rel="noopener noreferrer"&gt;Remove de Fundo&lt;/a&gt;, 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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;End result on the same laptop and the same photo: 43.8 s → 6.4 s from click to cutout.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Astro&lt;/strong&gt; for the site (static pages, deployed to Cloudflare Workers)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/imgly/background-removal-js" rel="noopener noreferrer"&gt;@imgly/background-removal&lt;/a&gt;&lt;/strong&gt; with the &lt;strong&gt;IS-Net fp16&lt;/strong&gt; model (~88 MB)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ONNX Runtime Web&lt;/strong&gt;, which runs the model with &lt;strong&gt;WebGPU&lt;/strong&gt; on desktop and &lt;strong&gt;WebAssembly&lt;/strong&gt; (CPU) on phones&lt;/li&gt;
&lt;li&gt;A small post-processing step in canvas to clean up semi-transparent "haze" in the mask&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything happens client-side. The server only serves static files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 1: the model came from a CDN that didn't cache
&lt;/h2&gt;

&lt;p&gt;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 &lt;strong&gt;no &lt;code&gt;Cache-Control&lt;/code&gt; header&lt;/strong&gt;, and the edge marked it &lt;code&gt;cf-cache-status: DYNAMIC&lt;/code&gt;. So returning visitors were often downloading the model again, and nothing was cached near the user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; self-host the model files. A build script downloads the model chunks once and puts them in &lt;code&gt;public/ai/&amp;lt;version&amp;gt;/&lt;/code&gt;, and the library is pointed there with &lt;code&gt;publicPath&lt;/code&gt;. Since the chunk file names are content hashes, they can be cached forever:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ai/*
  Cache-Control: public, max-age=31536000, immutable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now a returning visitor loads the model from disk cache: zero network.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;&amp;lt;hash&amp;gt;.bin&lt;/code&gt; and rewriting the names in &lt;code&gt;resources.json&lt;/code&gt; fixed it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2: the CPU path used one core out of eight
&lt;/h2&gt;

&lt;p&gt;Phones always use the WebAssembly path, so CPU speed matters most. ONNX Runtime Web can use multiple threads, but &lt;strong&gt;only if the page is cross-origin isolated&lt;/strong&gt;, because threads need &lt;code&gt;SharedArrayBuffer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My page wasn't isolated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;crossOriginIsolated&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; two headers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nc"&gt;Cross&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Origin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Opener&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Policy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;same&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;origin&lt;/span&gt;
&lt;span class="nc"&gt;Cross&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Origin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Embedder&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Policy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;credentialless&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used &lt;code&gt;credentialless&lt;/code&gt; instead of &lt;code&gt;require-corp&lt;/code&gt; so Google Fonts and analytics keep working without CORP headers. Browsers that don't support it simply ignore it, so nothing breaks there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result (CPU, model already loaded): 22.7 s → 7.9 s per photo.&lt;/strong&gt; Almost 3x, from two lines of config.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 3: the bug that made PNG encoding 13x slower
&lt;/h2&gt;

&lt;p&gt;After enabling isolation, inference got much faster but the total barely improved. I logged a timestamp for each stage and found this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Inference&lt;/td&gt;
&lt;td&gt;1.5 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PNG encode&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5.5 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Edge refinement&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;4.4 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Encoding a 1200 × 800 PNG should take a fraction of a second. I measured &lt;code&gt;canvas.convertToBlob()&lt;/code&gt; on an idle page:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Page&lt;/th&gt;
&lt;th&gt;PNG encode&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Not isolated&lt;/td&gt;
&lt;td&gt;208 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Isolated, before loading ONNX&lt;/td&gt;
&lt;td&gt;217 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Isolated, after running ONNX&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2,648 ms&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So isolation itself was fine. The problem: the library sets the WASM thread count to &lt;code&gt;navigator.hardwareConcurrency&lt;/code&gt; (all cores), and &lt;strong&gt;ONNX's worker threads keep spin-waiting after inference&lt;/strong&gt;. They occupy every core, and the browser's own image-encoding threads starve.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; 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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;limitOnnxThreads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'gpu'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s1"&gt;'cpu'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hardwareConcurrency&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;threads&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;device&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'gpu'&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cores&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;defineProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'hardwareConcurrency'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;configurable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Post-processing went from ~10 s back to &lt;strong&gt;0.2 s&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 4: the first photo on WebGPU paid for shader compilation
&lt;/h2&gt;

&lt;p&gt;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 &lt;strong&gt;first inference compile the GPU shaders&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; warm up on intent. When the user hovers the upload area (desktop) or taps it (mobile), I:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prefetch the model files into the HTTP cache (low priority).&lt;/li&gt;
&lt;li&gt;Call the library's &lt;code&gt;preload()&lt;/code&gt; with &lt;strong&gt;exactly the same config&lt;/strong&gt; as the real run (the library memoizes the session by &lt;code&gt;JSON.stringify(config)&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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 &lt;code&gt;saveData&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things I tried that didn't work
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The smaller quantized model (&lt;code&gt;isnet_quint8&lt;/code&gt;, 44 MB).&lt;/strong&gt; Half the download, but it left patches of background all over a portrait. Not worth it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Relying on HTTP heuristic caching&lt;/strong&gt; for a third-party CDN. In practice, returning visitors still re-downloaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  UX fixes that mattered as much as speed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The upload box was below the fold&lt;/strong&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Show the user's own photo&lt;/strong&gt; with a scanning line while processing, instead of a generic spinner. It makes the wait feel much shorter.&lt;/li&gt;
&lt;li&gt;On mobile, the label "Drag your photo here" makes no sense. It now says "Choose a photo".&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Click → result (laptop, first run)&lt;/td&gt;
&lt;td&gt;43.8 s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;6.4 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CPU inference per photo&lt;/td&gt;
&lt;td&gt;22.7 s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;7.9 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PNG encode + edge cleanup&lt;/td&gt;
&lt;td&gt;~10 s (isolated, before fix)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.2 s&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Returning visitor model download&lt;/td&gt;
&lt;td&gt;often ~100 MB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 (disk cache)&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Since adding analytics, about 98% of removals succeed and more than half of the people who upload a photo download the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Credits
&lt;/h2&gt;

&lt;p&gt;The heavy lifting is done by &lt;a href="https://github.com/imgly/background-removal-js" rel="noopener noreferrer"&gt;IMG.LY's background-removal-js&lt;/a&gt; (AGPL-3.0) and &lt;a href="https://onnxruntime.ai/" rel="noopener noreferrer"&gt;ONNX Runtime Web&lt;/a&gt;. If you build something similar, check their licenses.&lt;/p&gt;

&lt;p&gt;You can try it here: &lt;strong&gt;&lt;a href="https://removedefundo.com/en/" rel="noopener noreferrer"&gt;removedefundo.com/en&lt;/a&gt;&lt;/strong&gt;. It's free, with no sign-up, and works in English, Spanish and Portuguese. I'd love feedback, especially from people on older phones.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post was written with AI assistance. The numbers come from real measurements on the live site.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>ai</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
