<?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: Sahadat Husain</title>
    <description>The latest articles on DEV Community by Sahadat Husain (@dev-sahadat).</description>
    <link>https://dev.to/dev-sahadat</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3237762%2F0ad837fc-a08d-458f-be54-d8b22d0d0a99.webp</url>
      <title>DEV Community: Sahadat Husain</title>
      <link>https://dev.to/dev-sahadat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dev-sahadat"/>
    <language>en</language>
    <item>
      <title>JPEG quality 100 is not lossless: what 80, 90 and 100 cost on one photo</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Thu, 26 Nov 2026 16:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/jpeg-quality-100-is-not-lossless-what-80-90-and-100-cost-on-one-photo-14jj</link>
      <guid>https://dev.to/dev-sahadat/jpeg-quality-100-is-not-lossless-what-80-90-and-100-cost-on-one-photo-14jj</guid>
      <description>&lt;p&gt;An image compression test: a JPEG saved at quality 100 still changed 32.9% of the colour values in our test photo. Quality 100 is not lossless. It is just expensive.&lt;/p&gt;

&lt;p&gt;Same 4964 × 2782 photo, every file kept all its pixels:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fid53d1v0ozogjxjwwgq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fid53d1v0ozogjxjwwgq7.png" alt="One photo down the JPEG quality ladder" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;q100: 5,276.8 KB&lt;/li&gt;
&lt;li&gt;q90: 1,138.1 KB&lt;/li&gt;
&lt;li&gt;q80: 668.0 KB&lt;/li&gt;
&lt;li&gt;q75: 542.3 KB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Going from 100 to 80 drops 87% of the bytes. At 2x zoom, side by side, q80 and q90 look the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why 100 still loses
&lt;/h2&gt;

&lt;p&gt;JPEG throws data away in two steps that quality doesn't control:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Chroma subsampling.&lt;/strong&gt; Many encoders (Pillow and sharp by default) store colour at half resolution, even at q100.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Colour conversion and rounding.&lt;/strong&gt; RGB to YCbCr and back rounds every pixel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Quality only scales the quantization tables. At 100 they are all 1s, but the other two losses remain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Re-saving: the first save does the damage
&lt;/h2&gt;

&lt;p&gt;We saved the photo at q75 ten times in a row. The first save did almost all the damage. Later saves at the &lt;em&gt;same&lt;/em&gt; quality in the &lt;em&gt;same&lt;/em&gt; encoder barely changed it (253.3 KB → 251.9 KB on our test image). A save at a different quality, or in another app, rounds differently and loses more.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to ship
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Screens:&lt;/strong&gt; q80 is plenty. Try q75 for big heroes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prints and heavy crops:&lt;/strong&gt; q90.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;q100:&lt;/strong&gt; almost never. If you need exact pixels, use PNG or lossless WebP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Quick test for your next image: save it at 80 and at 90, open both at 100% zoom, keep the smaller one unless you can point at the difference.&lt;/p&gt;

&lt;p&gt;Do you hard-code a quality in your pipeline, or let the encoder choose per image?&lt;/p&gt;

&lt;p&gt;The same photo taken all the way down to quality 1, with crops: &lt;a href="https://blinkimg.com/lower-image-quality?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=jpeg-q100" rel="noopener noreferrer"&gt;https://blinkimg.com/lower-image-quality?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=jpeg-q100&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>images</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Pillow WebP method 0 to 6: what each level saves and costs</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Thu, 26 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/pillow-webp-method-0-to-6-what-each-level-saves-and-costs-3c0a</link>
      <guid>https://dev.to/dev-sahadat/pillow-webp-method-0-to-6-what-each-level-saves-and-costs-3c0a</guid>
      <description>&lt;p&gt;Pillow's WebP &lt;code&gt;method&lt;/code&gt; argument trades CPU for bytes, and the default of 4 is a fair middle. On a 1920 × 1076 photo at quality 80, &lt;code&gt;method=6&lt;/code&gt; wrote 101,962 bytes, 4.6% less than the default 106,836, and took 195 ms instead of 135 ms. &lt;code&gt;method=0&lt;/code&gt; was fastest at 34 ms but wrote 130,560 bytes, 22.2% more. The image quality score was nearly flat across all seven levels.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does method control?
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp" rel="noopener noreferrer"&gt;Pillow WebP docs&lt;/a&gt; describe it as a quality/speed trade-off, "0=fast, 6=slower-better", defaulting to 4. It does not change the &lt;code&gt;quality&lt;/code&gt; number you pass. In effect it sets how much effort the encoder spends at that quality: the same quality target, more effort, fewer bytes. I only timed the encode. I did not time decoding.&lt;/p&gt;

&lt;h2&gt;
  
  
  The script
&lt;/h2&gt;

&lt;p&gt;It encodes one photo at quality 80 for every method, times each encode and scores the decoded result against the source with PSNR.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line saves without a &lt;code&gt;method&lt;/code&gt; argument. It matched method 4 byte for byte (106,836 B), which confirms the documented default.&lt;/p&gt;

&lt;p&gt;PSNR (peak signal-to-noise ratio) is a rough number: higher means closer to the source,. It does not replace looking at the image. Timing uses &lt;code&gt;time.process_time()&lt;/code&gt; (CPU time), median of 5, encode only.&lt;/p&gt;

&lt;h2&gt;
  
  
  What did it print?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F038cdavolwzyfhp8iyc2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F038cdavolwzyfhp8iyc2.png" alt="Bars: WebP size in KB falls from 130.6 at method 0 to 102.0 at method 6, while CPU time rises from 33.9 ms to 195.1 ms" width="800" height="810"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where is the sweet spot?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Method 0 to 1 save time and cost bytes.&lt;/strong&gt; Method 0 took 33.9 ms and wrote 22.2% more than the default. Method 1 took 42.3 ms and wrote 14.1% more. Use these only when encode time is the real limit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method 2 is cheap and close.&lt;/strong&gt; It took 59.1 ms and wrote 3.4% more than the default. That is less than half the time of method 4 for a small byte cost.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Methods 3 and 4 were the same here.&lt;/strong&gt; Method 3 wrote 106,638 bytes in 136.7 ms. Method 4 wrote 106,836 bytes in 135.0 ms. A 198 byte gap on one photo is noise, so do not read it as a rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method 5 and 6 pay for bytes with time.&lt;/strong&gt; Method 5 saved 2.0% for about the same time as method 4 (137.7 ms). Method 6 saved 4.6% but cost about 45% more CPU than the default (195.1 ms against 135.0 ms).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Method 5 stands out: in this run it was nearly free. It took 2.7 ms longer than method 4 and saved 2,154 bytes. I would test it on my own photos before trusting that, since encoder time can swing with image content.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; PSNR stayed between 38.46 dB and 38.67 dB for all seven methods. The ranking by bytes is not a ranking by quality, and method 6 had the lowest PSNR (38.46 dB) of the lot, 0.19 dB under the default. If you chase the smallest file, check the picture too.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How big is the method effect next to quality?
&lt;/h2&gt;

&lt;p&gt;Smaller than a few quality steps. The whole method range, 0 to 6, spans 28,598 bytes (130,560 down to 101,962). I also encoded the same photo at &lt;code&gt;method=0&lt;/code&gt; and raised quality from 80 to 85. That added 40,468 bytes (130,560 to 171,028) and lifted PSNR from 38.53 dB to 39.65 dB. So quality moves both size and score. Method moves size and leaves the score almost where it was. That makes &lt;code&gt;method&lt;/code&gt; the safer knob when you cannot afford to change how the picture looks, and &lt;code&gt;quality&lt;/code&gt; the knob to use when you can.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which method should you use?
&lt;/h2&gt;

&lt;p&gt;For a request-time encode, where a user waits, use method 2 or the default. For a build step or a queue worker that runs once per image and serves it a million times, use method 6. A 4.6% saving on a 106,836 byte file is 4,874 bytes per view, and the CPU is spent once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;photo.webp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;WEBP&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quality&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you would rather not manage encoder settings, the &lt;a href="https://blinkimg.com/convert/jpg-to-webp" rel="noopener noreferrer"&gt;JPG to WebP converter&lt;/a&gt; converts without any code, and the &lt;a href="https://blinkimg.com/blog/what-is-webp" rel="noopener noreferrer"&gt;WebP explainer&lt;/a&gt; covers what the format can and cannot do.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Machine: Apple M1 Pro, macOS, 2026-10-18.&lt;/li&gt;
&lt;li&gt;Python 3.9.6 in a fresh venv, Pillow 11.3.0 with libwebp 1.5.0 (from &lt;code&gt;PIL.features&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Input: &lt;code&gt;image-compressor-sample-camera-photo.jpeg&lt;/code&gt; (4964 × 2782) resized to 1920 × 1076 with LANCZOS.&lt;/li&gt;
&lt;li&gt;Times: CPU time, median of 5 encodes. The machine was busy with other jobs, so I did not use the wall clock.&lt;/li&gt;
&lt;li&gt;PSNR: decoded WebP against the 1920 × 1076 RGB source, computed with NumPy.&lt;/li&gt;
&lt;li&gt;Bytes are exact and repeat from run to run. The times will differ on your machine.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;Other photos, other quality values, lossless WebP, images with alpha, and what the output looks like at 100% zoom.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webp</category>
      <category>performance</category>
      <category>images</category>
    </item>
    <item>
      <title>Read image width and height without decoding, in Node</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Wed, 25 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/read-image-width-and-height-without-decoding-in-node-nip</link>
      <guid>https://dev.to/dev-sahadat/read-image-width-and-height-without-decoding-in-node-nip</guid>
      <description>&lt;p&gt;You can read an image's width and height from its first few hundred bytes, without decoding a single pixel. For PNG, GIF and WebP the numbers sit within the first 32 bytes. For a 9.8 MB JPEG they sat within the first 672. A 40-line parser answered in 0.113 ms; a full decode of the same file took 217.3 ms.&lt;/p&gt;

&lt;p&gt;This matters at an upload endpoint: you can reject a 40,000 × 40,000 file before it costs you any memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where do the four formats keep their size?
&lt;/h2&gt;

&lt;p&gt;Each one stores it differently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PNG:&lt;/strong&gt; the first chunk is always IHDR (&lt;a href="https://www.w3.org/TR/png-3/" rel="noopener noreferrer"&gt;PNG spec&lt;/a&gt;: width, height, bit depth, colour type). Width is a big-endian 32-bit integer at byte 16, height at byte 20.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GIF:&lt;/strong&gt; the logical screen descriptor. Width and height are little-endian 16-bit integers at bytes 6 and 8.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebP:&lt;/strong&gt; a RIFF container with three layouts. Lossy (&lt;code&gt;VP8&lt;/code&gt;), lossless (&lt;code&gt;VP8L&lt;/code&gt;) and extended (&lt;code&gt;VP8X&lt;/code&gt;) each pack the size differently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JPEG:&lt;/strong&gt; no fixed offset. You walk the segments until you reach a start-of-frame marker (&lt;code&gt;SOF0&lt;/code&gt; to &lt;code&gt;SOF15&lt;/code&gt;, minus a few), which holds height then width.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I did not trust my memory of these offsets, so the next section checks every one against sharp on real files.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the parser look like?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// dims.cjs&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;dimensions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// PNG: signature, then IHDR. Big-endian width at 16, height at 20.&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt32BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mh"&gt;0x89504e47&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt32BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt32BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&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="c1"&gt;// GIF: "GIF8", then little-endian width and height at 6 and 8.&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;latin1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&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="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GIF8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gif&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16LE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16LE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&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="c1"&gt;// JPEG: walk the segments until a start-of-frame marker.&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;buf&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="o"&gt;===&lt;/span&gt; &lt;span class="mh"&gt;0xd8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&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="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;continue&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="nx"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;continue&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="nx"&gt;sof&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xc0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;marker&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mh"&gt;0xcf&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mh"&gt;0xc4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xc8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xcc&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sof&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jpeg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&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="c1"&gt;// skip this segment&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// not enough bytes yet: read more and retry&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// WebP: RIFF....WEBP, then one of three chunk types.&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;latin1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&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="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;RIFF&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;latin1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;WEBP&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;latin1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;VP8 &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16LE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x3fff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt16LE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x3fff&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;VP8L&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUInt32LE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x3fff&lt;/span&gt;&lt;span class="p"&gt;)&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="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0x3fff&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;VP8X&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUIntLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&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="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readUIntLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&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;The JPEG branch skips &lt;code&gt;DHT&lt;/code&gt; (0xc4), &lt;code&gt;JPG&lt;/code&gt; (0xc8) and &lt;code&gt;DAC&lt;/code&gt; (0xcc), which sit in the same marker range but are not frame headers. The pointer jumps by each segment's length, so it never touches the compressed data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does it match sharp?
&lt;/h2&gt;

&lt;p&gt;I ran it on the first 64 KB of each file and compared with &lt;code&gt;sharp(file).metadata()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image-compressor-sample-camera-photo.jpeg {"format":"jpeg","height":2782,"width":4964} sharp 4964x2782 match
jpg-sample-photo-original.jpg      {"format":"jpeg","height":1280,"width":1920} sharp 1920x1280 match
photo.png                          {"format":"png","width":800,"height":600} sharp 800x600 match
screen.png                         {"format":"png","width":3456,"height":22060} sharp 3456x22060 match
photo-lossy.webp                   {"format":"webp","width":1920,"height":1280} sharp 1920x1280 match
photo-lossless.webp                {"format":"webp","width":1920,"height":1280} sharp 1920x1280 match
transparent-alpha.webp             {"format":"webp","width":512,"height":512} sharp 512x512 match
IMG_7426.GIF                       {"format":"gif","width":373,"height":498} sharp 373x498 match
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All 12 files I ran matched (eight are shown), including the three WebP layouts I produced with sharp (lossy, lossless, lossy with alpha). &lt;code&gt;screen.png&lt;/code&gt; is a full-page screenshot, 3456 × 22060, and the parser still needed only 24 bytes.&lt;/p&gt;

&lt;h2&gt;
  
  
  How many bytes did it need?
&lt;/h2&gt;

&lt;p&gt;I grew the prefix in 32-byte steps until &lt;code&gt;dimensions()&lt;/code&gt; returned a result.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PNG, WebP and GIF: the first 32-byte step was enough.&lt;/li&gt;
&lt;li&gt;The 1920 × 1280 JPEG: 192 bytes.&lt;/li&gt;
&lt;li&gt;The 9.8 MB camera JPEG: 672 bytes. It carries an ICC profile segment (474 bytes) ahead of the frame header.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A JPEG with a big EXIF block or an embedded thumbnail pushes the frame header further out. That is why the function returns &lt;code&gt;null&lt;/code&gt; instead of guessing: read more bytes and call it again.&lt;/p&gt;

&lt;h2&gt;
  
  
  How much faster is it?
&lt;/h2&gt;

&lt;p&gt;Median of 5 runs, in milliseconds. "Header" is reading 64 KB from disk plus the parse.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwnqzdwdgyfp8jewv7r3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwnqzdwdgyfp8jewv7r3i.png" alt="Median milliseconds to get width and height by header parse, sharp metadata and a full decode" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File&lt;/th&gt;
&lt;th&gt;Header&lt;/th&gt;
&lt;th&gt;&lt;code&gt;metadata()&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Full decode&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;camera.jpeg (9.8 MB)&lt;/td&gt;
&lt;td&gt;0.113&lt;/td&gt;
&lt;td&gt;3.948&lt;/td&gt;
&lt;td&gt;217.3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;photo-1920.jpg&lt;/td&gt;
&lt;td&gt;0.084&lt;/td&gt;
&lt;td&gt;1.276&lt;/td&gt;
&lt;td&gt;19.2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;screen.png (8.3 MB)&lt;/td&gt;
&lt;td&gt;0.076&lt;/td&gt;
&lt;td&gt;2.836&lt;/td&gt;
&lt;td&gt;265.2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;photo-lossy.webp&lt;/td&gt;
&lt;td&gt;0.138&lt;/td&gt;
&lt;td&gt;1.435&lt;/td&gt;
&lt;td&gt;41.8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IMG_7426.GIF&lt;/td&gt;
&lt;td&gt;0.043&lt;/td&gt;
&lt;td&gt;0.592&lt;/td&gt;
&lt;td&gt;3.0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;metadata()&lt;/code&gt; is already lazy. It reads the header and stops, which is why it is far cheaper than a decode. The hand-rolled parser is about 10 to 37 times faster than &lt;code&gt;metadata()&lt;/code&gt; on these files, but both are tiny next to the decode. Use sharp unless you handle many thousands of files per second, or you want the check before sharp is involved at all.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; the header is the file's claim, not the truth. The size says 4964 × 2782, and a truncated or hostile file may hold fewer pixels, or a different format behind a valid header. The parser tells you what to reject early. It does not tell you the file decodes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What does EXIF orientation do to it?
&lt;/h2&gt;

&lt;p&gt;Nothing, and that is the trap. I wrote a copy of the 800 × 600 test photo with EXIF orientation 6 (rotate 90°):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"format":"jpeg","height":600,"width":800}   800 600 6 {"width":600,"height":800}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parser and &lt;code&gt;metadata()&lt;/code&gt; both say 800 × 600. The picture displays as 600 × 800, and sharp reports that under &lt;code&gt;autoOrient&lt;/code&gt;. If you compare an upload against a pixel limit, the product is the same either way. If you check an aspect ratio, swap width and height when orientation is 5 to 8.&lt;/p&gt;

&lt;h2&gt;
  
  
  What about AVIF and HEIC?
&lt;/h2&gt;

&lt;p&gt;I did not write those parsers. Their size lives in an &lt;code&gt;ispe&lt;/code&gt; box inside nested ISO-BMFF boxes, and the walk is longer than a few offsets. Let sharp read those headers with &lt;code&gt;metadata()&lt;/code&gt;, which stays cheap.&lt;/p&gt;

&lt;h2&gt;
  
  
  One measurement trap
&lt;/h2&gt;

&lt;p&gt;sharp caches operations by default. My first decode timings for WebP and GIF looked absurdly low (1 ms and 3 ms) because repeat runs hit the cache. Call &lt;code&gt;sharp.cache(false)&lt;/code&gt; before you time anything. The table above uses that setting.&lt;/p&gt;

&lt;p&gt;For the format-detection side of the same bytes, see the &lt;a href="https://blinkimg.com/blog/image-formats-explained" rel="noopener noreferrer"&gt;image formats guide&lt;/a&gt;. For how we run measurements, see &lt;a href="https://blinkimg.com/how-we-measure" rel="noopener noreferrer"&gt;how we measure&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Node v24.22.0, sharp 0.35.4 (libvips 8.18.6), Apple M1 Pro, 2026-10-18.&lt;/li&gt;
&lt;li&gt;Files: the site's 4964 × 2782 camera photo, the 1920 × 1280 sample photo, the engine fixtures &lt;code&gt;photo.jpg&lt;/code&gt;, &lt;code&gt;photo.png&lt;/code&gt;, &lt;code&gt;screen.png&lt;/code&gt;, &lt;code&gt;photo.webp&lt;/code&gt;, and three WebP files and one GIF I wrote or had on disk (IMG_7426.GIF, 2,817,469 bytes).&lt;/li&gt;
&lt;li&gt;Timings: median of 5 runs, &lt;code&gt;sharp.cache(false)&lt;/code&gt;. Full decode is &lt;code&gt;sharp(file).raw().toBuffer()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Raw numbers: &lt;code&gt;measured.json&lt;/code&gt; in this folder.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;AVIF, HEIC, TIFF, BMP and JPEG XL headers. Progressive and arithmetic-coded JPEGs beyond the marker list above. Files whose headers lie about their size.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>images</category>
      <category>webdev</category>
    </item>
    <item>
      <title>sharp resize kernels compared: lanczos3, mitchell, nearest by PSNR</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Tue, 24 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/sharp-resize-kernels-compared-lanczos3-mitchell-nearest-by-psnr-46de</link>
      <guid>https://dev.to/dev-sahadat/sharp-resize-kernels-compared-lanczos3-mitchell-nearest-by-psnr-46de</guid>
      <description>&lt;p&gt;Keep sharp's default, &lt;code&gt;lanczos3&lt;/code&gt;. When we shrank a 1920 × 1076 photo to a quarter of its size, lanczos3 kept the most detail (33.02 dB PSNR) and linear kept the least of the smooth kernels (32.29 dB). The price is bytes: the lanczos3 JPEG was 20.5 KB and the linear one 19.0 KB, 7.8% less. Resize speed barely differed, at a few milliseconds each.&lt;/p&gt;

&lt;p&gt;Below are the eight kernels, the test, and the one result that looks wrong: &lt;code&gt;nearest&lt;/code&gt; scoring highest at half size.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the resize kernels in sharp?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;kernel&lt;/code&gt; option in &lt;a href="https://sharp.pixelplumbing.com/api-resize/#resize" rel="noopener noreferrer"&gt;&lt;code&gt;resize()&lt;/code&gt;&lt;/a&gt; picks the filter used when an image shrinks. The sharp docs list eight: &lt;code&gt;nearest&lt;/code&gt;, &lt;code&gt;linear&lt;/code&gt; (a triangle filter), &lt;code&gt;cubic&lt;/code&gt; (Catmull-Rom), &lt;code&gt;mitchell&lt;/code&gt;, &lt;code&gt;lanczos2&lt;/code&gt;, &lt;code&gt;lanczos3&lt;/code&gt; (the default), &lt;code&gt;mks2013&lt;/code&gt; and &lt;code&gt;mks2021&lt;/code&gt; (Magic Kernel Sharp). Sharper filters keep fine detail but can ring around hard edges. Softer filters blur a little and compress better.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you score a resize kernel?
&lt;/h2&gt;

&lt;p&gt;A resized image has no ground truth to compare against. So I used a round trip. Shrink with the kernel, enlarge back to the original size with lanczos3, and compute PSNR against the original pixels. A higher score means the small image kept more of what the big one had.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;sharp&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sharp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1920&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeAlpha&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;resolveWithObject&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;kernel&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nearest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;linear&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cubic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mitchell&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lanczos2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lanczos3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mks2013&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mks2021&lt;/span&gt;&lt;span class="dl"&gt;'&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="nx"&gt;w&lt;/span&gt; &lt;span class="o"&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;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&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="nx"&gt;h&lt;/span&gt; &lt;span class="o"&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;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&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="nx"&gt;small&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;kernel&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;back&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;kernel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lanczos3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;back&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;back&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;psnr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&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;log10&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;65025&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;back&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jpeg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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="nf"&gt;jpeg&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;kernel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;psnr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jpeg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toFixed&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;KB&lt;/span&gt;&lt;span class="dl"&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;The photo is our 4964 × 2782 camera photo (a lake at sunset), resized to 1920 × 1076 first. This is what the 1/4 run printed for the 480 × 269 result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nearest   32.31 dB   22.3 KB
linear    32.29 dB   19.0 KB
cubic     32.87 dB   20.1 KB
mitchell  32.53 dB   19.3 KB
lanczos2  32.88 dB   20.1 KB
lanczos3  33.02 dB   20.5 KB
mks2013   33.01 dB   21.2 KB
mks2021   32.98 dB   20.4 KB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjlyh88ykni9rd4oqvp7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjlyh88ykni9rd4oqvp7b.png" alt="Bars of resize kernel PSNR and output bytes for a photo and a screenshot shrunk to a quarter of their size" width="800" height="888"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Which kernel keeps the most detail?
&lt;/h2&gt;

&lt;p&gt;On the photo at 1/4 size, three kernels sit within 0.04 dB of each other at the top: lanczos3 (33.02), mks2013 (33.01) and mks2021 (32.98). Treat them as tied. Below them come lanczos2 and cubic at 32.88 and 32.87 dB, then mitchell at 32.53 and linear at 32.29.&lt;/p&gt;

&lt;p&gt;The spread from best to worst is 0.73 dB. Mitchell is the interesting middle. It is 19.3 KB against 20.5 KB for lanczos3, a 5.5% saving for 0.49 dB less.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why did nearest win at half size?
&lt;/h2&gt;

&lt;p&gt;At 1/2 size (960 × 538) the photo results flip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nearest   37.29 dB   64.3 KB
linear    34.96 dB   56.8 KB
cubic     35.27 dB   59.9 KB
mitchell  35.10 dB   57.8 KB
lanczos2  35.28 dB   60.0 KB
lanczos3  35.32 dB   61.2 KB
mks2013   35.17 dB   63.4 KB
mks2021   35.32 dB   60.8 KB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nearest scored 37.29 dB, 1.97 dB above lanczos3. I do not trust that as quality. At an exact 2:1 ratio, nearest keeps every other pixel untouched, and PSNR rewards exact copies. It does not penalise the jagged edges and shimmer that nearest adds on diagonal lines. At 1/4 size, where it can no longer do that, nearest scored 32.31 dB and fell to the bottom.&lt;/p&gt;

&lt;p&gt;Nearest also wrote the biggest JPEG at both sizes (64.3 KB and 22.3 KB), probably because its hard pixel steps are costly to encode. I did not test that.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Do not pick a kernel from PSNR alone. PSNR measures pixel error, not what a person sees. If one number surprises you, as nearest did, look at the picture.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What about screenshots with text?
&lt;/h2&gt;

&lt;p&gt;I ran the same test on our pricing page screenshot, 1440 × 900, saving PNG output (so bytes track edge detail). At 1/4 size (360 × 225):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nearest   22.19 dB   31.4 KB
linear    23.12 dB   34.8 KB
cubic     23.48 dB   38.4 KB
mitchell  23.25 dB   40.6 KB
lanczos2  23.49 dB   38.3 KB
lanczos3  23.57 dB   42.1 KB
mks2013   23.62 dB   39.4 KB
mks2021   23.56 dB   40.0 KB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here mks2013 edged out lanczos3 on both scores: 23.62 dB at 39.4 KB against 23.57 dB at 42.1 KB. That is 6.3% fewer bytes and a slightly higher score. It is one screenshot, so I would read it as a hint, not a rule. Nearest again wins at half size (27.43 dB against 25.91 dB for lanczos3) and loses at a quarter.&lt;/p&gt;

&lt;h2&gt;
  
  
  How long does each kernel take?
&lt;/h2&gt;

&lt;p&gt;Each resize ran 5 times and I kept the median. On the 1920 × 1076 photo, the 1/2 shrink took between 3.3 ms (nearest) and 8.3 ms (mks2013). Lanczos3 took 7.9 ms and linear 4.1 ms. At 1/4 size every kernel took between 4.2 ms and 8.5 ms, apart from lanczos2 with a 22.8 ms median I cannot explain and did not repeat.&lt;/p&gt;

&lt;p&gt;At these sizes the kernel is not a performance decision. Pick it for the picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which kernel should you use?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Photos for the web:&lt;/strong&gt; leave &lt;code&gt;lanczos3&lt;/code&gt;. It scored top or tied for top at both shrink ratios.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Photos where bytes matter more than the last 0.5 dB:&lt;/strong&gt; try &lt;code&gt;mitchell&lt;/code&gt;, which was 5.5% smaller at 1/4 size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;UI screenshots and text:&lt;/strong&gt; test &lt;code&gt;mks2013&lt;/code&gt; against lanczos3 on your own images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pixel art that must stay crisp:&lt;/strong&gt; &lt;code&gt;nearest&lt;/code&gt;, at integer ratios only.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our &lt;a href="https://blinkimg.com/blog/how-to-resize-an-image-without-losing-quality" rel="noopener noreferrer"&gt;resize guide&lt;/a&gt; also scores sharp's kernels on this camera photo. It uses a different test, so its ranking of the smooth kernels differs from mine; read both before you change a default.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Photo: &lt;code&gt;apps/web/public/images/index/image-compressor-sample-camera-photo.jpeg&lt;/code&gt;, 4964 × 2782, resized to 1920 × 1076 as the reference.&lt;/li&gt;
&lt;li&gt;Screenshot: &lt;code&gt;apps/web/assets/reference/screenshot.png&lt;/code&gt; (our pricing page), resized to 1440 × 900 as the reference.&lt;/li&gt;
&lt;li&gt;Versions: Node 24.22.0, sharp 0.35.4, libvips 8.18.6.&lt;/li&gt;
&lt;li&gt;Score: shrink with each kernel, enlarge back with lanczos3, PSNR over RGB against the reference. Size: JPEG quality 80 for the photo, default PNG for the screenshot.&lt;/li&gt;
&lt;li&gt;Time: resize only, from raw pixels, median of 5 runs. PSNR and bytes: one run per cell.&lt;/li&gt;
&lt;li&gt;Machine: Apple M1 Pro, macOS, 2026-10-18. Harness: &lt;code&gt;apps/web/scripts/devto-images/_measure-sharp-tuning.mjs&lt;/code&gt;; the blocks above are its &lt;code&gt;measured.json&lt;/code&gt;, reformatted.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;I did not run SSIM or any perceptual metric, view the outputs side by side, test enlarging with a kernel, or use &lt;code&gt;fastShrinkOnLoad&lt;/code&gt; on JPEG input. Ratios other than 1/2 and 1/4 and a second photo are untested.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>performance</category>
      <category>images</category>
    </item>
    <item>
      <title>Decoding a 50 MP JPEG vs a PNG in sharp: peak memory, measured</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Mon, 23 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/decoding-a-50-mp-jpeg-vs-a-png-in-sharp-peak-memory-measured-5fk9</link>
      <guid>https://dev.to/dev-sahadat/decoding-a-50-mp-jpeg-vs-a-png-in-sharp-peak-memory-measured-5fk9</guid>
      <description>&lt;p&gt;A 50 megapixel image costs about 150 MB of raw pixels in sharp, and the process peaked at 264 MB for a JPEG and 253 MB for a PNG when I decoded it in full. A resize to 1920 pixels is cheaper: 134 MB for the 3.5 MB JPEG, 184 MB for the 65.5 MB PNG. Reading the size with &lt;code&gt;metadata()&lt;/code&gt; added nothing over an idle process (72 MB).&lt;/p&gt;

&lt;p&gt;Plan your memory per pixel, not per file byte. Here is the script, the output, and what it means for how many images a worker can hold at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  How much memory does a 50 megapixel image need?
&lt;/h2&gt;

&lt;p&gt;The raw pixel count sets the floor. 8000 × 6250 is 50,000,000 pixels. At 3 bytes per pixel (RGB) that is 150,000,000 bytes, and sharp returned exactly that from &lt;code&gt;raw().toBuffer()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I built three test images from our camera photo (4964 × 2782, &lt;code&gt;apps/web/public/images/index/image-compressor-sample-camera-photo.jpeg&lt;/code&gt;), upscaled to 8000 × 6250:&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="c1"&gt;// gen.mjs&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;sharp&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sharp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image-compressor-sample-camera-photo.jpeg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cover&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;noise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6250&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;channels&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;g&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="na"&gt;noise&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gaussian&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;sigma&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&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="nf"&gt;png&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;compressionLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mixed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;composite&lt;/span&gt;&lt;span class="p"&gt;([{&lt;/span&gt; &lt;span class="na"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;noise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;blend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;overlay&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}]).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mixed&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;jpeg&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;85&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;j50.jpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mixed&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;png&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;compressionLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p50.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;j50.jpg 8000 6250 50000000 3548643
p50.png 8000 6250 50000000 65506064
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same pixels are 3,548,643 bytes as a JPEG and 65,506,064 bytes as a PNG. The noise makes the PNG large; a real screenshot would be far smaller, and I show a plainer PNG below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does the file format change peak memory?
&lt;/h2&gt;

&lt;p&gt;I ran each operation in a fresh Node process and read the peak resident set size from &lt;code&gt;/usr/bin/time -l&lt;/code&gt;. Each cell is the median of 5 runs.&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="c1"&gt;// mem.mjs  (node mem.mjs &amp;lt;file&amp;gt; &amp;lt;op&amp;gt;)&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;sharp&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sharp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readFile&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:fs/promises&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;op&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;metadata&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resize&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1920&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;webp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;resize-buffer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1920&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;webp&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;raw&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;JPEG 3.5 MB&lt;/th&gt;
&lt;th&gt;PNG 65.5 MB&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;idle Node with sharp loaded&lt;/td&gt;
&lt;td&gt;72 MB&lt;/td&gt;
&lt;td&gt;72 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;metadata()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;72 MB, 1.2 ms&lt;/td&gt;
&lt;td&gt;72 MB, 1.0 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;resize(1920).webp()&lt;/code&gt; from a path&lt;/td&gt;
&lt;td&gt;134 MB, 307.5 ms&lt;/td&gt;
&lt;td&gt;184 MB, 871.6 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;same, input read into a &lt;code&gt;Buffer&lt;/code&gt; first&lt;/td&gt;
&lt;td&gt;134 MB, 290.1 ms&lt;/td&gt;
&lt;td&gt;256 MB, 743.7 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;raw().toBuffer()&lt;/code&gt; (full decode)&lt;/td&gt;
&lt;td&gt;264 MB, 122.1 ms&lt;/td&gt;
&lt;td&gt;253 MB, 490.8 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fixfm93zvrzoxoplbqbt3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fixfm93zvrzoxoplbqbt3.png" alt="Peak memory for the JPEG and the PNG under resize, full decode and metadata" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Three things stand out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A full decode costs the same for both formats.&lt;/strong&gt; The output buffer is 150 MB either way, and the process peaks 192 MB (JPEG) and 181 MB (PNG) above idle. The format does not change what you pay once you ask for every pixel.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A resize is cheaper than a full decode.&lt;/strong&gt; For the JPEG it peaked at 134 MB, half of the 264 MB. This fits libvips decoding the JPEG at a reduced size, but I did not confirm that directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Passing a Buffer instead of a path costs the file size on top.&lt;/strong&gt; The PNG went from 184 MB to 256 MB, a gap of 72 MB, close to its 65.5 MB size. The 3.5 MB JPEG did not move.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; a small file is not a small decode. Our plain upscaled PNG (no noise, 69,569,834 bytes) peaked at 186 MB for a resize and 258 MB for a full decode, the same band as the noisy one. File size predicts almost nothing. Pixels do.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What does this mean for a worker?
&lt;/h2&gt;

&lt;p&gt;Take the 264 MB full decode and a 2 GB container. Subtract roughly 72 MB for the runtime and you can hold about seven such decodes at once, before any output or encoder memory. The resize path at 134 to 184 MB fits ten or more. Those are my arithmetic on my numbers, not a limit I measured on a small server.&lt;/p&gt;

&lt;p&gt;So cap two things. Cap the pixel count (50,000,000 here) and cap how many images are in flight. The &lt;a href="https://sharp.pixelplumbing.com/api-constructor/" rel="noopener noreferrer"&gt;sharp constructor docs&lt;/a&gt; give &lt;code&gt;limitInputPixels&lt;/code&gt; a default of 268,402,689, which at 3 bytes per pixel would be 805 MB for one raw buffer. That is too loose for a small worker. For the pixel-cap behaviour in detail, see my earlier piece on &lt;a href="https://dev.to/dev-sahadat/limitinputpixels-and-maximagepixels-pixel-caps-tested-57d8"&gt;limitInputPixels and MAX_IMAGE_PIXELS&lt;/a&gt;. Reading the size first is cheap, as the table shows: 1.2 ms and no extra memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you do?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Set &lt;code&gt;limitInputPixels&lt;/code&gt; to what your product needs. A 50 MP cap is 150 MB of raw pixels.&lt;/li&gt;
&lt;li&gt;Read &lt;code&gt;metadata()&lt;/code&gt; first and reject early. It added no memory in my runs.&lt;/li&gt;
&lt;li&gt;Pass a file path when you can. A Buffer adds the whole file to the heap.&lt;/li&gt;
&lt;li&gt;Budget a full decode at about 3 bytes per pixel plus the runtime, not the file size.&lt;/li&gt;
&lt;li&gt;Bound parallel jobs. Memory grows with each one in flight.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our own &lt;a href="https://blinkimg.com/how-we-measure" rel="noopener noreferrer"&gt;engine&lt;/a&gt; works under these limits, and the &lt;a href="https://blinkimg.com/blog/400-megapixel-jpeg-on-a-small-server" rel="noopener noreferrer"&gt;400 MP JPEG case&lt;/a&gt; shows what happens past them.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Machine: Apple M1 Pro, 8 cores, 16 GB, macOS, 2026-10-18.&lt;/li&gt;
&lt;li&gt;Versions: Node 24.22.0, sharp 0.35.4, libvips 8.18.6.&lt;/li&gt;
&lt;li&gt;Peak memory: maximum resident set size from &lt;code&gt;/usr/bin/time -l&lt;/code&gt;, median of 5 fresh Node processes per cell. Times are the median of the same 5 runs, measured inside the process.&lt;/li&gt;
&lt;li&gt;Raw output in &lt;code&gt;measured.json&lt;/code&gt; next to this article.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;One photo's pixels, and one platform. Linux, other allocators and other sharp versions will give different numbers. I did not test AVIF, WebP or HEIC input, or 16-bit images.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>performance</category>
      <category>security</category>
    </item>
    <item>
      <title>Stop shipping WebP as your default in 2026: AVIF was 12–43% smaller on every photo we tested</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Sun, 22 Nov 2026 16:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/stop-shipping-webp-as-your-default-in-2026-avif-was-12-43-smaller-on-every-photo-we-tested-53h0</link>
      <guid>https://dev.to/dev-sahadat/stop-shipping-webp-as-your-default-in-2026-avif-was-12-43-smaller-on-every-photo-we-tested-53h0</guid>
      <description>&lt;p&gt;A web performance hot take: WebP is still the default format in most image pipelines, and in 2026 that leaves bytes on the table. AVIF beat WebP on every photo we encoded. WebP should be your fallback, not your first choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers
&lt;/h2&gt;

&lt;p&gt;Same engine, Auto quality, three photos:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff9f5fdwz940k5z2av1rp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff9f5fdwz940k5z2av1rp.png" alt="AVIF vs WebP: who wins where" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Photo-like test image, 1920 × 1280:&lt;/strong&gt; WebP 218.9 KB, AVIF 123.9 KB. AVIF is 43% smaller.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hero photo, 1920 × 1076:&lt;/strong&gt; WebP 85.5 KB, AVIF 69.3 KB. AVIF is 19% smaller.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Camera photo, full 4964 × 2782:&lt;/strong&gt; WebP 329 KB, AVIF 288 KB. AVIF is 12% smaller.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  "But support"
&lt;/h2&gt;

&lt;p&gt;That's no longer an argument. caniuse puts AVIF at 95.35% of users and WebP at 96.74% (August 2026 data). That's a 1.4-point gap, and &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; covers it with a fallback.&lt;/p&gt;

&lt;p&gt;Yet the 2025 Web Almanac found only 0.7% of LCP images in AVIF. The format is ready. Our pipelines aren't.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where WebP still wins (be honest about it)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Flat graphics.&lt;/strong&gt; On a simple 640 × 480 flat-colour PNG, WebP wrote 0.2 KB and AVIF wrote 0.8 KB. For logos, icons and UI art, WebP lossless or SVG wins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encode time.&lt;/strong&gt; AVIF is slower. On our server, one 9.1 MP iPhone photo took 1.36 s at effort 3 and 2.44 s at effort 4. Cloudflare's docs say AVIF encoding "can be an order of magnitude slower". Encode once at upload or build time and cache it. Don't encode per request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quality numbers don't transfer.&lt;/strong&gt; AVIF q80 is not WebP q80. Copy your WebP setting over and you can end up with a bigger file. Tune AVIF on its own scale.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What a sane default looks like
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"image/avif"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"photo-800.avif 800w, photo-1600.avif 1600w"&lt;/span&gt; &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"100vw"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"image/webp"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"photo-800.webp 800w, photo-1600.webp 1600w"&lt;/span&gt; &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"100vw"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"photo-1600.jpg"&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1600"&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"900"&lt;/span&gt; &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"..."&lt;/span&gt; &lt;span class="na"&gt;loading&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"lazy"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Photos: AVIF first, WebP second, JPEG last. Flat art: SVG or lossless WebP.&lt;/p&gt;

&lt;p&gt;Change my mind. What's actually stopping you from making AVIF the default: encode time, your CDN, your CMS, or something else?&lt;/p&gt;

&lt;p&gt;Method and full comparison: &lt;a href="https://blinkimg.com/blog/avif-vs-webp-vs-jpeg?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=stop-webp-default" rel="noopener noreferrer"&gt;https://blinkimg.com/blog/avif-vs-webp-vs-jpeg?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=stop-webp-default&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>discuss</category>
      <category>images</category>
    </item>
    <item>
      <title>16-bit PNG in sharp silently becomes 8-bit: bytes and banding</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Sun, 22 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/16-bit-png-in-sharp-silently-becomes-8-bit-bytes-and-banding-4m7b</link>
      <guid>https://dev.to/dev-sahadat/16-bit-png-in-sharp-silently-becomes-8-bit-bytes-and-banding-4m7b</guid>
      <description>&lt;p&gt;sharp converts a 16-bit PNG to 8-bit unless you ask for 16 bits. In my test, a 10,358,752-byte 16-bit PNG came back as a 66,111-byte 8-bit PNG from a plain &lt;code&gt;.png()&lt;/code&gt; call. The green channel dropped from 11,496 distinct levels to 46. To keep 16 bits, add &lt;code&gt;.toColourspace('rgb16')&lt;/code&gt; before &lt;code&gt;.png()&lt;/code&gt;. That gave 9,605,628 bytes.&lt;/p&gt;

&lt;p&gt;Whether the loss matters depends on the image. Here is what it costs and how to check.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I make a real 16-bit test file?
&lt;/h2&gt;

&lt;p&gt;Most "16-bit" files I could find were 8-bit photos widened to 16 bits, which tests nothing. I wrote a PNG by hand: a dim, smooth sky (green from 1,080 to 12,595 of 65,535) with 2 levels of noise, 1920 × 1280. This is a synthetic gradient, not a photo.&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="c1"&gt;// make16.cjs: write a true 16-bit RGB PNG&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;zlib&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:zlib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1920&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;H&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1280&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;6&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;H&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;seed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12345&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rnd&lt;/span&gt; &lt;span class="o"&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="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;seed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;seed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1664525&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1013904223&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;H&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// filter type 0&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;++&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="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;H&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;12800&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeUInt16BE&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;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;rnd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeUInt16BE&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;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;rnd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;o&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="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeUInt16BE&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;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;base&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;rnd&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;o&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="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="nx"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&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="nx"&gt;len&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeUInt32BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;crc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&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="nx"&gt;crc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeUInt32BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;zlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;crc32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;len&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;crc&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="nx"&gt;ihdr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ihdr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeUInt32BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;W&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nx"&gt;ihdr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeUInt32BE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;H&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="nx"&gt;ihdr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;ihdr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&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="c1"&gt;// 16 bits per sample, colour type 2 (RGB)&lt;/span&gt;
&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sky16.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mh"&gt;0x89&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x4e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x47&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x1a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0a&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
  &lt;span class="nf"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IHDR&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ihdr&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IDAT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;zlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deflateSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;})),&lt;/span&gt; &lt;span class="nf"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;IEND&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;&lt;code&gt;zlib.crc32&lt;/code&gt; needs Node 22 or newer. Bit depth and colour type live at bytes 24 and 25 of any PNG (the IHDR chunk comes first, per the &lt;a href="https://www.w3.org/TR/png-3/" rel="noopener noreferrer"&gt;PNG spec&lt;/a&gt;), so a one-line &lt;code&gt;ihdr(buf)&lt;/code&gt; helper can tell you what a file really is.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does a plain sharp round trip do?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sky16.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;same&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;png&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;compressionLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;keep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sharp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toColourspace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rgb16&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;png&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;compressionLevel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;toBuffer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source             {"bytes":10358752,"bitDepth":16,"colourType":2,"sharpDepth":"ushort","bitsPerSample":16}
default re-encode  {"bytes":66111,"bitDepth":8,"colourType":2}
toColourspace srgb {"bytes":66111,"bitDepth":8,"colourType":2}
keep16             {"bytes":9605628,"bitDepth":16,"colourType":2}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;sharp reads the file as 16-bit (&lt;code&gt;depth: 'ushort'&lt;/code&gt;) and still writes 8-bit. The docs say it plainly: "By default, PNG output is full colour at 8 bits per pixel," and for 16 bits "convert to &lt;code&gt;rgb16&lt;/code&gt; via toColourspace" (&lt;a href="https://sharp.pixelplumbing.com/api-output/" rel="noopener noreferrer"&gt;output options&lt;/a&gt;). The &lt;code&gt;toColourspace&lt;/code&gt; entry shows the same &lt;code&gt;rgb16&lt;/code&gt; example (&lt;a href="https://sharp.pixelplumbing.com/api-colour/" rel="noopener noreferrer"&gt;colour&lt;/a&gt;). I only found that paragraph after the file got smaller.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; the same default hides in other calls. &lt;code&gt;.raw({ depth: 'ushort' })&lt;/code&gt; on my 16-bit file gave green values with a maximum of 49 instead of 12,595, because the pipeline had already gone to 8-bit. Add &lt;code&gt;.toColourspace('rgb16')&lt;/code&gt; before you read raw 16-bit data too.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What did the conversion cost?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd2t9kuca601mvqj3dcix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd2t9kuca601mvqj3dcix.png" alt="Bytes of one 16-bit sky through sharp: 16-bit PNG, 8-bit PNG, palette PNG and JPEG" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;16-bit PNG source: 10,358,752 bytes&lt;/li&gt;
&lt;li&gt;16-bit re-encode with &lt;code&gt;rgb16&lt;/code&gt;: 9,605,628 bytes&lt;/li&gt;
&lt;li&gt;8-bit PNG (default): 66,111 bytes&lt;/li&gt;
&lt;li&gt;8-bit palette PNG (&lt;code&gt;palette: true, quality: 90&lt;/code&gt;): 142,786 bytes&lt;/li&gt;
&lt;li&gt;JPEG q90 (mozjpeg): 16,188 bytes&lt;/li&gt;
&lt;li&gt;WebP q90: 11,562 bytes&lt;/li&gt;
&lt;li&gt;AVIF q60: 3,142 bytes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The 16-bit file is 157 times larger than the 8-bit one. The reason is the noise. My 2 levels of noise are real information at 16 bits and invisible at 8, so the 8-bit file compresses to almost nothing.&lt;/p&gt;

&lt;p&gt;Counting levels in the green channel shows what was thrown away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;distinct green 16-bit 11496 range 1080 12595 8-bit 46
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 8-bit file has 46 green levels for the whole sky. On the middle row, the longest run of identical green values is 143 pixels. That is the banding you see in dark skies and vignettes after an 8-bit save.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is it always worth keeping 16 bits?
&lt;/h2&gt;

&lt;p&gt;No. I also tested the 1920 × 1280 sample photo. It is an 8-bit JPEG, so widening it adds no information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;photo {"png16":{"bytes":4999854,"bitDepth":16,"colourType":2},"png8":{"bytes":3944916,"bitDepth":8,"colourType":2}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 16-bit copy is 4,999,854 bytes against 3,944,916, 26.7% larger, with no new information in it. Keep 16 bits when the source really has them: scans, RAW exports, HDR renders, grading work. For the web, convert to 8-bit on purpose, and dither or add grain first if the image has smooth gradients.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should I do in a pipeline?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Read &lt;code&gt;metadata().depth&lt;/code&gt;. &lt;code&gt;ushort&lt;/code&gt; means a 16-bit source.&lt;/li&gt;
&lt;li&gt;If you serve it on the web, the 8-bit default is fine, but log it so it is a decision.&lt;/li&gt;
&lt;li&gt;If it is a master you keep, write it with &lt;code&gt;toColourspace('rgb16')&lt;/code&gt; and test with &lt;code&gt;ihdr(buf)&lt;/code&gt; that byte 24 is 16.&lt;/li&gt;
&lt;li&gt;Never judge an 8-bit result by file size alone. 66,111 bytes looked like a success until I counted the levels.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More format background is in the &lt;a href="https://blinkimg.com/blog/what-is-png" rel="noopener noreferrer"&gt;PNG guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Node v24.22.0, sharp 0.35.4 (libvips 8.18.6, libpng 1.6.58), Apple M1 Pro, 2026-10-18.&lt;/li&gt;
&lt;li&gt;Inputs: the hand-written 1920 × 1280 sky above and &lt;code&gt;jpg-sample-photo-original.jpg&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Single runs; encoding is deterministic. Levels counted over every pixel.&lt;/li&gt;
&lt;li&gt;Raw numbers: &lt;code&gt;measured.json&lt;/code&gt; in this folder.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;Real 16-bit photographs. Grayscale and 16-bit RGBA PNGs. TIFF 16-bit output. Visual banding on a monitor; I counted levels, I did not view the file on a screen.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>images</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Fail CI when an image is over its byte budget (Node script)</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Sat, 21 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/fail-ci-when-an-image-is-over-its-byte-budget-node-script-5b78</link>
      <guid>https://dev.to/dev-sahadat/fail-ci-when-an-image-is-over-its-byte-budget-node-script-5b78</guid>
      <description>&lt;p&gt;A byte budget for images is a small script that walks a folder, compares each file to a limit for its format, and exits with code 1 when any file is over. CI treats a non-zero exit as a failed check. We ran ours on a real 1,585-image folder: it flagged 24 files and the total was 85,778,075 bytes against a 12,000,000 byte budget.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why check bytes in CI at all?
&lt;/h2&gt;

&lt;p&gt;Nobody reviews a binary diff. A reviewer sees "Bin 0 -&amp;gt; 9804300 bytes" and approves. One camera photo committed straight from a phone is 9.8 MB, and it ships to every visitor until someone notices.&lt;/p&gt;

&lt;p&gt;A budget turns that into a failing check on the pull request. The rule is written down, so the argument happens once, in a config, not in every review.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the script look like?
&lt;/h2&gt;

&lt;p&gt;It needs no dependencies. It walks the tree with &lt;code&gt;readdir&lt;/code&gt;, reads each size with &lt;code&gt;stat&lt;/code&gt;, and compares it to a limit chosen by extension. We picked 250,000 bytes for JPEG, 300,000 for PNG, 150,000 for WebP and AVIF, and 12,000,000 for the whole folder. These are our numbers, not a standard. Pick yours from your own pages.&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="c1"&gt;// check-image-budget.mjs: fail CI when an image is over its byte budget.&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readdir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;stat&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:fs/promises&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;extname&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node:path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;public/images&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BUDGET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.jpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.jpeg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.png&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.webp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.avif&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="nx"&gt;_000&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TOTAL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="nx"&gt;_000_000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;readdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;withFileTypes&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="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isDirectory&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="nx"&gt;p&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;checked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;over&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&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="nx"&gt;limit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BUDGET&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;extname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()];&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;checked&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;over&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;over&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;over&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`FAIL &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; bytes (limit &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, over by &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;)`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;checked&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; images, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; bytes total (budget &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;TOTAL&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;), &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;over&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; over their limit`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TOTAL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`FAIL total over by &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;TOTAL&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; bytes`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;over&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;TOTAL&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter. The script sorts offenders by size, so the worst file is the first line of the log. And it sets the exit code explicitly: a script that only prints "FAIL" still passes CI.&lt;/p&gt;

&lt;h2&gt;
  
  
  What did it print on a real folder?
&lt;/h2&gt;

&lt;p&gt;We ran it on the image folder of this site (&lt;code&gt;apps/web/public/images&lt;/code&gt;). The first lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FAIL images/index/image-compressor-sample-camera-photo.jpeg  9804300 bytes (limit 250000, over by 9554300)
FAIL images/convert/heic-to-png-converter-result.png  997215 bytes (limit 300000, over by 697215)
FAIL images/tools/crop-png-online-result.png  705491 bytes (limit 300000, over by 405491)
FAIL images/convert/webp-sample-photo-original.webp  611228 bytes (limit 150000, over by 461228)
...
1585 images, 85778075 bytes total (budget 12000000), 24 over their limit
FAIL total over by 73778075 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F80me9s0usaf9n75bm7vq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F80me9s0usaf9n75bm7vq.png" alt="The four largest files over budget, led by a 9,804.3 KB camera photo" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of those files are deliberate. This folder holds the original photos that our tool pages compress, so they are big on purpose. That is the point of a budget: it forces a choice. You raise the limit for a named file, or you shrink the file.&lt;/p&gt;

&lt;p&gt;A smaller folder shows the passing case. &lt;code&gt;images/pricing&lt;/code&gt; has one image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 images, 9736 bytes total (budget 12000000), 0 over their limit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exit code was 0.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you run it in CI?
&lt;/h2&gt;

&lt;p&gt;Any CI runs a command and reads its exit code. Here is a GitHub Actions job. We did not run this workflow file; we ran the script it calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;image-budget&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pull_request&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;budget&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-node@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;node-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;24&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node scripts/check-image-budget.mjs public/images&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check the current action versions in &lt;a href="https://docs.github.com/en/actions" rel="noopener noreferrer"&gt;GitHub's docs&lt;/a&gt; before you copy this.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you handle files you cannot shrink?
&lt;/h2&gt;

&lt;p&gt;Add an allowlist: a map from path to a higher limit, with a comment saying why. Keep it in the repo so it shows up in review. Do not raise the global limit to fit one file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Check the bytes the user downloads, not the bytes in git. If your build resizes or converts images, run the check on the build output folder too. A source file at 9.8 MB can be fine if the build emits a 173 KB variant, and a small source can still become a large output.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If a file fails, our guide on &lt;a href="https://blinkimg.com/blog/how-to-reduce-image-file-size" rel="noopener noreferrer"&gt;reducing image file size&lt;/a&gt; covers the fixes in order of effort, and the pre-commit hook approach in our other article catches the file before it reaches CI.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Date: 2026-10-18. Machine: Apple M1 Pro, macOS 15.7.3.&lt;/li&gt;
&lt;li&gt;Node 24.22.0, sharp 0.35.4, libvips 8.18.6.&lt;/li&gt;
&lt;li&gt;KB = 1,000 bytes. Raw numbers are in &lt;code&gt;measured.json&lt;/code&gt; next to this article.&lt;/li&gt;
&lt;li&gt;The script above, unchanged, run with &lt;code&gt;node&lt;/code&gt; from &lt;code&gt;apps/web/public&lt;/code&gt; on &lt;code&gt;images&lt;/code&gt; and on &lt;code&gt;images/pricing&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;I did not run the GitHub Actions workflow. I did not check dimensions or pixel counts, only bytes.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>performance</category>
      <category>ci</category>
    </item>
    <item>
      <title>Never lazy-load your LCP image: the fetchpriority, preload and srcset checklist</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Fri, 20 Nov 2026 16:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/never-lazy-load-your-lcp-image-the-fetchpriority-preload-and-srcset-checklist-4kp8</link>
      <guid>https://dev.to/dev-sahadat/never-lazy-load-your-lcp-image-the-fetchpriority-preload-and-srcset-checklist-4kp8</guid>
      <description>&lt;p&gt;A Core Web Vitals tip: never lazy-load the image that decides your LCP (Largest Contentful Paint). Give it &lt;code&gt;fetchpriority="high"&lt;/code&gt;, leave &lt;code&gt;loading&lt;/code&gt; out, and add a preload only when the HTML can't show the image.&lt;/p&gt;

&lt;p&gt;The 2025 Web Almanac still finds 16–17% of pages lazy-loading their LCP image. Only 17% of mobile pages with an image LCP use &lt;code&gt;fetchpriority="high"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each attribute actually does
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;loading="lazy"&lt;/code&gt;: the download waits for layout. On a hero, that wait is pure delay.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;loading="eager"&lt;/code&gt;: the default. Writing it changes nothing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fetchpriority="high"&lt;/code&gt;: the request starts at High instead of Low. Google Flights went from 2.6 s to 1.9 s LCP with it (web.dev).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lazy&lt;/code&gt; + &lt;code&gt;high&lt;/code&gt;: still waits for layout. The delay stays.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When you need a preload
&lt;/h2&gt;

&lt;p&gt;Only when the preload scanner can't see the image:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a CSS &lt;code&gt;background-image&lt;/code&gt; hero&lt;/li&gt;
&lt;li&gt;an image a script inserts&lt;/li&gt;
&lt;li&gt;a JS-built slider&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Put &lt;code&gt;fetchpriority="high"&lt;/code&gt; on the preload too. Match &lt;code&gt;imagesrcset&lt;/code&gt; and &lt;code&gt;imagesizes&lt;/code&gt; to the element exactly, or the browser downloads twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Then cut the bytes
&lt;/h2&gt;

&lt;p&gt;Same hero photo, 1920 px wide, our engine at Auto quality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JPEG: 140.3 KB&lt;/li&gt;
&lt;li&gt;WebP: 85.5 KB&lt;/li&gt;
&lt;li&gt;AVIF: 69.3 KB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A phone that shows the hero at 400 CSS px on a 2x screen needs 800 px. That file is 35.2 KB. &lt;code&gt;srcset&lt;/code&gt; + a real &lt;code&gt;sizes&lt;/code&gt; value gets it there. Note that &lt;code&gt;sizes="auto"&lt;/code&gt; only works with lazy images, so your LCP image needs a real value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo8mk5g9hplvb2wfu0ic8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo8mk5g9hplvb2wfu0ic8.png" alt="Which fix shortens which part of LCP" width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The markup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
  &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/img/hero-1280.jpg"&lt;/span&gt;
  &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/img/hero-800.jpg 800w, /img/hero-1280.jpg 1280w, /img/hero-1920.jpg 1920w"&lt;/span&gt;
  &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"100vw"&lt;/span&gt;
  &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1920"&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1076"&lt;/span&gt;
  &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"..."&lt;/span&gt;
  &lt;span class="na"&gt;fetchpriority&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"high"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrap it in &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; with an AVIF &lt;code&gt;&amp;lt;source&amp;gt;&lt;/code&gt; first, then WebP.&lt;/p&gt;

&lt;p&gt;What does your LCP breakdown look like? I'm curious which sub-part hurts most on real sites: load delay or load duration.&lt;/p&gt;

&lt;p&gt;Full write-up with every byte count: &lt;a href="https://blinkimg.com/blog/image-optimization?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=lcp-checklist" rel="noopener noreferrer"&gt;https://blinkimg.com/blog/image-optimization?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=lcp-checklist&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>html</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The picture element: which format each browser downloads</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Fri, 20 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/the-picture-element-which-format-each-browser-downloads-2e6l</link>
      <guid>https://dev.to/dev-sahadat/the-picture-element-which-format-each-browser-downloads-2e6l</guid>
      <description>&lt;p&gt;A &lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; element makes each browser download exactly one file. In my test, Chromium 149, Firefox 151 and WebKit 26.5 all picked the AVIF: 34,898 bytes, against 79,671 for the JPEG fallback. That is 44,773 bytes fewer, or 56.2%. The savings depend on two things people get wrong: source order and the &lt;code&gt;type&lt;/code&gt; attribute.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the browser download from a picture element?
&lt;/h2&gt;

&lt;p&gt;It walks the &lt;code&gt;&amp;lt;source&amp;gt;&lt;/code&gt; children in order and takes the first one it can use. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/picture" rel="noopener noreferrer"&gt;MDN's picture reference&lt;/a&gt; says a source with a &lt;code&gt;type&lt;/code&gt; the browser does not support is skipped, and the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; is the fallback when no source matches. The first match wins. The rest are never requested.&lt;/p&gt;

&lt;p&gt;I made five files from one 4964 × 2782 camera photo, resized to 1280 px wide: mozjpeg q80, WebP q75 and AVIF q50 (effort 4). Then I served three pages from a local Node server that logs every response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"image/avif"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"p-1280.avif"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"image/webp"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"p-1280.webp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"p-1280.jpg"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"1280"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"717"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwz09thfpieedhcdmqecr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwz09thfpieedhcdmqecr.png" alt="Bytes downloaded for JPEG only, WebP first and AVIF first" width="800" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JPEG only: 79,671 bytes.&lt;/li&gt;
&lt;li&gt;WebP source first: 46,384 bytes (41.8% fewer than JPEG).&lt;/li&gt;
&lt;li&gt;AVIF source first: 34,898 bytes (56.2% fewer).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chromium, Firefox and WebKit gave the same result on all three pages. The server log showed one image request each time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does source order change the bytes?
&lt;/h2&gt;

&lt;p&gt;Yes, and nothing warns you. I swapped the two sources so WebP came first. Every browser took the WebP and never looked at the AVIF. That cost 11,486 extra bytes, 32.9% more than the AVIF. List the smallest format first.&lt;/p&gt;

&lt;h2&gt;
  
  
  What if the browser does not support a type?
&lt;/h2&gt;

&lt;p&gt;All three engines I have installed support AVIF, so I could not watch a real fallback. I simulated one with a made-up type, &lt;code&gt;image/x-blinkimg-test&lt;/code&gt;. This is the page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"image/x-blinkimg-test"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"p-1280.avif"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"image/x-blinkimg-test2"&lt;/span&gt; &lt;span class="na"&gt;srcset=&lt;/span&gt;&lt;span class="s"&gt;"p-1280.webp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"p-1280.jpg"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"1280"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"717"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three browsers skipped both sources and downloaded the JPEG: 79,671 bytes. No request went out for the skipped files. An old browser behaves the same way with &lt;code&gt;image/avif&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;type&lt;/code&gt; is a promise about the file. If the browser supports the type and the file is missing, there is no fallback. I put &lt;code&gt;type="image/jxl"&lt;/code&gt; first and pointed it at a file that does not exist. Chromium and Firefox skipped it and loaded the AVIF. WebKit 26.5 matched the type, requested the missing file and rendered nothing (&lt;code&gt;naturalWidth&lt;/code&gt; was 0). The &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; never ran.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Do you need the type attribute?
&lt;/h2&gt;

&lt;p&gt;Without &lt;code&gt;type&lt;/code&gt;, the browser cannot skip a source by format. It only skips on &lt;code&gt;media&lt;/code&gt; or when it fails to decode. In my run a type-less AVIF source worked, because all three engines decode AVIF. On an engine that cannot, you would get a broken image, not a JPEG. Always write the type.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do you check what loaded?
&lt;/h2&gt;

&lt;p&gt;Read &lt;code&gt;currentSrc&lt;/code&gt; from the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; in the console:&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;picture img&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;currentSrc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a page you do not own, the &lt;a href="https://blinkimg.com/analyze" rel="noopener noreferrer"&gt;page image analyzer&lt;/a&gt; lists each image's format and weight.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does the AVIF win hold at every width?
&lt;/h2&gt;

&lt;p&gt;I made the three formats at five widths from the same photo. AVIF was smallest every time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;320 px: AVIF 4,656, WebP 6,316, JPEG 9,589 bytes.&lt;/li&gt;
&lt;li&gt;640 px: AVIF 12,558, WebP 16,862, JPEG 27,154.&lt;/li&gt;
&lt;li&gt;960 px: AVIF 21,926, WebP 28,936, JPEG 50,630.&lt;/li&gt;
&lt;li&gt;1280 px: AVIF 34,898, WebP 46,384, JPEG 79,671.&lt;/li&gt;
&lt;li&gt;1920 px: AVIF 66,799, WebP 85,468, JPEG 153,394.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The gap grows with width. At 320 px the AVIF saves 4,933 bytes over the JPEG. At 1920 px it saves 86,595. These are my quality settings (q50 for AVIF is not equal to q80 for JPEG in every photo), so compare by eye before you ship.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is the file size the only cost?
&lt;/h2&gt;

&lt;p&gt;No. A smaller AVIF can still cost more to decode on a weak phone, and I did not time decoding. The byte count is what I measured. Treat it as the network half of the answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should you do?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Put AVIF first, WebP second, JPEG in the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Write the right &lt;code&gt;type&lt;/code&gt; on every source.&lt;/li&gt;
&lt;li&gt;Keep &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; on the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;. The browser uses them for the layout box.&lt;/li&gt;
&lt;li&gt;Make sure every file in a &lt;code&gt;&amp;lt;source&amp;gt;&lt;/code&gt; exists.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For making the three files, the &lt;a href="https://blinkimg.com/blog/image-optimization" rel="noopener noreferrer"&gt;image optimization guide&lt;/a&gt; covers when each format wins.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Apple Silicon Mac, 2026-10-18. Node 24.22.0, sharp 0.35.4 (libvips 8.18.6), Playwright 1.61.1, Chromium 149.0.7827.55, Firefox 151.0, WebKit 26.5.&lt;/li&gt;
&lt;li&gt;Source: &lt;code&gt;image-compressor-sample-camera-photo.jpeg&lt;/code&gt;, 4964 × 2782, 9,804,300 bytes. Resized to 1280 px wide.&lt;/li&gt;
&lt;li&gt;Bytes are response body sizes from a local server log (no headers, no compression). KB = 1,000 bytes.&lt;/li&gt;
&lt;li&gt;Raw output: &lt;code&gt;measured.json&lt;/code&gt; in this folder.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;I did not test a browser without AVIF support, only a fake type. I did not test a CDN, HTTP caching or a slow network.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>webperf</category>
      <category>images</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Pillow JPEG optimize=True and progressive: bytes and CPU time</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Thu, 19 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/pillow-jpeg-optimizetrue-and-progressive-bytes-and-cpu-time-47ph</link>
      <guid>https://dev.to/dev-sahadat/pillow-jpeg-optimizetrue-and-progressive-bytes-and-cpu-time-47ph</guid>
      <description>&lt;p&gt;Add &lt;code&gt;optimize=True&lt;/code&gt; to every Pillow JPEG save. On a 4964 × 2782 photo at quality 75 it cut the file from 772,155 bytes to 704,228 bytes (8.8% smaller) and the decoded pixels stayed identical. &lt;code&gt;progressive=True&lt;/code&gt; saved about the same or a little more, but took 4 times as much CPU as the plain save. Below are all four combinations at two sizes and two qualities.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do optimize and progressive actually change?
&lt;/h2&gt;

&lt;p&gt;Neither flag touches the pixels. Both change how the encoder writes the same quantised data. The &lt;a href="https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#jpeg" rel="noopener noreferrer"&gt;Pillow JPEG docs&lt;/a&gt; say &lt;code&gt;optimize&lt;/code&gt; makes the encoder "make an extra pass over the image in order to select optimal encoder settings", and &lt;code&gt;progressive&lt;/code&gt; stores the file as a progressive JPEG. In practice, &lt;code&gt;optimize&lt;/code&gt; builds Huffman tables fitted to this image instead of the generic default tables. &lt;code&gt;progressive&lt;/code&gt; splits the data into scans that refine the picture as it loads.&lt;/p&gt;

&lt;p&gt;So the question is not "does quality drop". It is "what do I pay in CPU for the bytes I save". I measured that.&lt;/p&gt;

&lt;h2&gt;
  
  
  The script
&lt;/h2&gt;

&lt;p&gt;It saves one image four ways (optimize on or off, progressive on or off), decodes each result and compares the pixels with the plain save, and times the encode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it as &lt;code&gt;python jpeg_flags.py camera.jpeg&lt;/code&gt;. The timer is &lt;code&gt;time.process_time()&lt;/code&gt;, which counts the CPU time of this process, so other jobs on the machine do not distort it. Each time is the median of 5 encodes, and the image is already decoded in memory, so only the encode is timed.&lt;/p&gt;

&lt;h2&gt;
  
  
  What did it print?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How much do the bytes change?
&lt;/h2&gt;

&lt;p&gt;Here are the savings against the plain save (all pixels identical):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Camera photo, quality 75:&lt;/strong&gt; optimize saved 8.8% (704,228 B), progressive saved 8.1% (709,747 B).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Camera photo, quality 85:&lt;/strong&gt; optimize saved 3.5% (1,069,842 B), progressive saved 4.9% (1,054,225 B).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web photo, quality 75:&lt;/strong&gt; optimize saved 3.8% (158,659 B), progressive saved 3.9% (158,522 B).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web photo, quality 85:&lt;/strong&gt; optimize saved 1.7% (225,068 B), progressive saved 2.5% (223,183 B).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fypc4b3ypqd596rj8gjao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fypc4b3ypqd596rj8gjao.png" alt="Bars: the camera photo at quality 75 is 772,155 bytes plain, 704,228 with optimize and 709,747 with progressive; CPU time is 27.8, 56.3 and 115.3 ms" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two things stand out. The saving shrinks as quality rises: at quality 85 the extra Huffman work has fewer small values to squeeze. And neither flag wins everywhere. At quality 75 on the camera photo, &lt;code&gt;optimize&lt;/code&gt; beat &lt;code&gt;progressive&lt;/code&gt; (704,228 B against 709,747 B). At quality 85 on the same photo, &lt;code&gt;progressive&lt;/code&gt; won (1,054,225 B against 1,069,842 B).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; the two progressive rows are byte-for-byte the same size whether &lt;code&gt;optimize&lt;/code&gt; is on or off (709,747 B in both camera q75 rows). In my runs &lt;code&gt;progressive=True&lt;/code&gt; gave the optimized result by itself, so &lt;code&gt;optimize=True, progressive=True&lt;/code&gt; costs nothing extra over &lt;code&gt;progressive=True&lt;/code&gt;. I did not read the encoder source to confirm why. I only saw the equal bytes in all four pairs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What does it cost in CPU time?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Camera photo, quality 75:&lt;/strong&gt; plain 27.8 ms, optimize 56.3 ms (2.0×), progressive 115.3 ms (4.1×).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web photo, quality 75:&lt;/strong&gt; plain 4.5 ms, optimize 10.1 ms (2.2×), progressive 20.9 ms (4.6×).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Optimize roughly doubles the encode. Progressive roughly quadruples it. For one upload that is nothing. For a batch of 10,000 full-size photos, the 59 ms gap between progressive and optimize adds about 10 minutes of CPU.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which flags should you use?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Always set &lt;code&gt;optimize=True&lt;/code&gt;.&lt;/strong&gt; It is lossless, it never made a file bigger in these runs, and it costs a few milliseconds per web-sized image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add &lt;code&gt;progressive=True&lt;/code&gt; for photos that travel over the web.&lt;/strong&gt; Progressive JPEGs paint a blurry full image first and sharpen as bytes arrive. Against &lt;code&gt;optimize&lt;/code&gt; alone it was smaller in 3 of the 4 cases here and larger in 1. The extra CPU is a one-time cost per file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Measure before you use progressive on tiny thumbnails.&lt;/strong&gt; I did not test any size below 1920 px wide.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;im&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;out.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;JPEG&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quality&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;optimize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;progressive&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check the result on your own files without writing code, run them through the &lt;a href="https://blinkimg.com/compress/jpg" rel="noopener noreferrer"&gt;JPG compressor&lt;/a&gt; and compare the bytes. The &lt;a href="https://blinkimg.com/blog/image-optimization" rel="noopener noreferrer"&gt;image optimization guide&lt;/a&gt; covers where this step fits in a full pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I measured
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Machine: Apple M1 Pro, macOS, 2026-10-18.&lt;/li&gt;
&lt;li&gt;Python 3.9.6 in a fresh venv, Pillow 11.3.0 with libjpeg-turbo 3.1.1 (from &lt;code&gt;PIL.features&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Input: &lt;code&gt;image-compressor-sample-camera-photo.jpeg&lt;/code&gt; from the site (4964 × 2782). The "web" photo is the same file resized to 1920 × 1076 with LANCZOS.&lt;/li&gt;
&lt;li&gt;Times: CPU time (&lt;code&gt;time.process_time()&lt;/code&gt;), median of 5, encode only. The machine was busy with other jobs while I ran this, which is why I used CPU time and not the wall clock.&lt;/li&gt;
&lt;li&gt;Pixel check: each output decoded back to RGB and compared with &lt;code&gt;numpy.array_equal&lt;/code&gt; against the plain save. All 16 matched.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I did not test
&lt;/h2&gt;

&lt;p&gt;Other quality values, CMYK or grayscale sources, mozjpeg (Pillow uses libjpeg-turbo here), and how fast a progressive file paints in a browser.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I build &lt;a href="https://blinkimg.com" rel="noopener noreferrer"&gt;Blinkimg&lt;/a&gt;, an image compressor and converter with a &lt;a href="https://blinkimg.com/api" rel="noopener noreferrer"&gt;developer API&lt;/a&gt;. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>performance</category>
      <category>images</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>AVIF vs WebP vs JPEG in 2026: one photo, same width, real bytes</title>
      <dc:creator>Sahadat Husain</dc:creator>
      <pubDate>Wed, 18 Nov 2026 16:00:00 +0000</pubDate>
      <link>https://dev.to/dev-sahadat/avif-vs-webp-vs-jpeg-in-2026-one-photo-same-width-real-bytes-35b6</link>
      <guid>https://dev.to/dev-sahadat/avif-vs-webp-vs-jpeg-in-2026-one-photo-same-width-real-bytes-35b6</guid>
      <description>&lt;p&gt;This is a web performance benchmark of image formats. AVIF came out 51% smaller than JPEG on our test photo at the same 1920 px width. WebP came out 39% smaller. Support is no longer the excuse: 95.35% of users can see AVIF.&lt;/p&gt;

&lt;p&gt;We took one 9.8 MB camera photo (4964 × 2782), resized it to 1920 × 1076, and encoded it with the same engine at Auto quality:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Format&lt;/th&gt;
&lt;th&gt;Bytes&lt;/th&gt;
&lt;th&gt;vs JPEG&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;JPEG q85&lt;/td&gt;
&lt;td&gt;183.7 KB&lt;/td&gt;
&lt;td&gt;+31%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JPEG Auto&lt;/td&gt;
&lt;td&gt;140.3 KB&lt;/td&gt;
&lt;td&gt;base&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;WebP Auto&lt;/td&gt;
&lt;td&gt;85.5 KB&lt;/td&gt;
&lt;td&gt;−39%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AVIF Auto&lt;/td&gt;
&lt;td&gt;69.3 KB&lt;/td&gt;
&lt;td&gt;−51%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqxrxoqr67x84tath6fk2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqxrxoqr67x84tath6fk2.png" alt="The 2026 web image format cheat sheet" width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Yet almost nobody ships it
&lt;/h2&gt;

&lt;p&gt;The 2025 Web Almanac found only 0.7% of LCP images in AVIF. WebP support sits at 96.74%, AVIF at 95.35% (caniuse, August 2026 data). The gap between "supported" and "shipped" is mostly build tooling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three things that trip people up
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Quality numbers don't transfer.&lt;/strong&gt; AVIF q80 is not JPEG q80. On one test image AVIF q80 weighed 159.5 KB, while JPEG q80 weighed 82.1 KB. Tune each format on its own scale.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;lt;picture&amp;gt;&lt;/code&gt; order matters.&lt;/strong&gt; The browser takes the first type it supports, not the smallest file. Put AVIF first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Measure decode on a cheap phone.&lt;/strong&gt; We measured bytes, not decode time. AVIF decode costs more CPU, and that lands in LCP's render delay.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The setup we ship
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;picture&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"image/avif"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hero-800.avif 800w, hero-1920.avif 1920w"&lt;/span&gt; &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"100vw"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;source&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"image/webp"&lt;/span&gt; &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hero-800.webp 800w, hero-1920.webp 1920w"&lt;/span&gt; &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"100vw"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"hero-1920.jpg"&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1920"&lt;/span&gt; &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"1076"&lt;/span&gt; &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"..."&lt;/span&gt; &lt;span class="na"&gt;fetchpriority&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"high"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/picture&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Are you serving AVIF in production yet? If not, what's blocking you: encode time, CDN, or the CMS?&lt;/p&gt;

&lt;p&gt;Method and the full comparison: &lt;a href="https://blinkimg.com/blog/avif-vs-webp-vs-jpeg?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=avif-webp-jpeg" rel="noopener noreferrer"&gt;https://blinkimg.com/blog/avif-vs-webp-vs-jpeg?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=avif-webp-jpeg&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>frontend</category>
      <category>images</category>
    </item>
  </channel>
</rss>
