Skip to content

Instantly share code, notes, and snippets.

@manfromanotherland
Last active July 20, 2017 22:33
Show Gist options
  • Save manfromanotherland/2b0c88115510fb7e2ee5fb65f9b87317 to your computer and use it in GitHub Desktop.
Save manfromanotherland/2b0c88115510fb7e2ee5fb65f9b87317 to your computer and use it in GitHub Desktop.
Image performance on the web

Image performance on the web

Some notes on image performance. What we'll cover:

  • The cost of images (Both on the Network & Render side)
  • Image srcset – what it does and how it works
  • Linting responsive images

The cost of images

Network

Use the Network Panel to understand the download time of images.

Network Panel

⭐ Use the larger-than Network panel filter to quickly find big resources. See list of filters in the official docs.

Render

The "render" cost, in our context, represents the time it takes for a browser to "draw" the image to the screen. Once an image has been downloaded from the network, the browser will spend more time decoding that image and drawing it to the screen. Traditional website performance recommendations focus heavily on the network. In 2017, with better developer tooling, we can begin to understanding timings after an image has been downloaded.

Image decode

Image Decode is an operation the browser performs on an image to prepare it for displaying to you, the user. An overlooked performance aspect with images is the duration of this operation. Note: Image decoding may take ~5ms with a small image on a powerful machine, however it's a different story on a low-end device + large image.

Performance panel

Note that the cost of image decoding on a say Nexus 4 is even greater. (see remote debugging)

Image srcset

Before diving into what srcset is, take some time to understand the problem it can solve.

Have a look at the images on the Poppulo landing page. For example the raw image email-solutions-card-[size].jpg has a width of 960px, however I only display it at 960px on a large screen. Also, on a narrow viewport size, for example when the viewport width is at 440px, the image width is constrained to 440px and resized accordingly.

Image srcset on Poppulo.com

The problem is when you have a high-quality image scaled down to the user (with CSS), regardless of what size it will be displayed at. It would be like downloading a 4K video from YouTube, but then watching it at 480p.

The srcset attribute on the img tag can help us solve this.

What srcset is

The srcset attribute extends the img element, so you can supply multiple image files for difference device characteristics.

How srcset works

<!-- You can say -->
<img src="image.jpg">

<!-- But with srcset, we can hint to the browser certain things -->
<img srcset="image-small.jpg 400w, image-medium.jpg 800w">

<!-- Device pixel ratio -->
<img srcset="image-small.jpg 1x, image-medium.jpg 2x">

Responsive image linting

I'm using these two tools that are quite hand on linting responsiveness of images:

  • respimagelint – Linter for responsive images
  • Lighthouse – Google tool for performance in general, also reports on responsive images.

Conclusion

On the website we are using imagemin with mozjpeg and optipng for optimizing the images without losing quality. Sadly that doesn't work on images uploaded through WordPress :(


Browser support - Supports all modern browsers BUT for IE (yes for Edge).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment