DEV Community

Yuwang Cai
Yuwang Cai

Posted on

Web Performance: Images

High-quality images can significantly boost your website's appeal. They make an excellent first impression on users and effectively communicate your insights.

However, according to Web Almanac 2022 statistics, images are also the leading cause of website performance issues, with a staggering page weight of 1000+ KB, which is twice that of JavaScript.

Images have twice the page weight of JavaScript

We want to deliver high-quality images while minimizing performance loss, which necessitates extensive and in-depth image optimization.

🔽 Reduce Image Quality

Among all possible solutions, brute force always seems to be the most intuitive one. The less information in an image, the easier it is to compress, the better the performance.

  1. Blur: If we only want to emphasize the foreground of image, we can blur the background, which reduces the amount of information while keeping all of the important parts.
  2. Zoom: Zoom out to 87% of the original image, and then zoom in to 115% of the current one. In the end, the size did not change, but many pixels vanished during the zooming process.
  3. Tool: Use tools like Squoosh to further compress the image.

After the above processing, the size of an image can be reduced to ~3%, with humans barely noticing the difference.

🎞️ Choose Correct Format

Scenario Format
Ordinary Pictures WebP, Jpeg
Complex graphics PNG, Jpeg
With transparency PNG, WebP
Icons SVG
Dynamic Video

🤔 Other

  • Adjust the width to somewhere between 320px and 1920px.
  • Opaque is preferred to transparent.
  • Remove all unnecessary shapes in SVG.
  • Integrate tools like Sharp and Imagemin into the build scripts.

🖼️ Responsive Images

If we provide a 1920px wide image for laptops, mobile devices will also receive the same 1920px wide image, which is completely unnecessary.

srcset is a native attribute of <img> that allows us to provide images of different sizes for devices of different widths. The size attribute can then be used to run media query based on these images.

<img
  srcset="image-small.jpg 320w   👈 for devices under 320px
          image-medium.jpg 600w  👈 for devices under 600px
          image-large.jpg 1200w  👈 for devices under 1200px
          image-full.jpg 1920w   👈 for devices under 1920px
         "
  size="(min-width:1200px) 1200px, 100vw  👈 1200px for devices above 1200px, or 100vw otherwise"
  src="image.jpg"
  alt="A nice image."
/>
Enter fullscreen mode Exit fullscreen mode

Typically, 4-5 options would suffice. Going too far is as bad as not going far enough.

🛏️ Lazy Loading

The image is not loaded until the user scrolls to it.

<img src="image.png" alt="A nice image." loading="lazy" />
Enter fullscreen mode Exit fullscreen mode

Simple as the attribute is, it can boost the performance a lot. loading="lazy" should be the default for every image.

Exception: It is not recommended to apply lazy loading to LCP. It would only have negative impacts on the performance.

Top comments (2)

Collapse
 
xsgxmicha3lx profile image
xSGxMICHA3Lx R6S

WebP is the best now! it's having everything we need.

Collapse
 
xsgxmicha3lx profile image
xSGxMICHA3Lx R6S

Thanks man! it saves me today.