brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: decoding="async" on an image lets the page paint before the image is decoded

Web-Development
~/tips/tip-html-img-decoding-async

width="800" height="600" decoding="async">

Hints that an image can be decoded after the rest of the page has painted.

Everyone knows loading="lazy" and width and height on images. A less known attribute is decoding, which is about the CPU work after the image has arrived.

It is a hint about when to decode the image: together with the rest of the page, or after it:

<img src="photo.jpg" alt="A mountain lake at dawn"
     width="800" height="600" decoding="async">

The three values:

  • sync: decode together with the rest of the content and present everything at once.
  • async: decode after the rest is rendered, so the next paint does not wait for the image.
  • auto: no preference, and the browser decides. This is the default.

It also works on images you create in script, where the effect is easier to notice:

const img = new Image();
img.decoding = "async";
img.src = "photo.jpg";
document.body.append(img);

Caveats: it is only a hint, and the browser may ignore it. On a normal static <img> the difference is small and hard to see, because the image is fetched before anything is shown anyway. MDN points to an analysis by tunetheweb.com on what the attribute actually does, and advises measuring before assuming a gain. The effect shows up most for big images added late through JavaScript, or long lists of images.

A sensible default is to put decoding="async" on images below the fold and leave the main hero image alone, since you want that one in the first paint. So I’d treat this as a small tweak to try and measure, not a fix for a slow page.

Further reading: MDN: <img> decoding attribute

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link