Fast and Furious Images. Web Performance tuning tips.
Tags: javascript, webdev, programming, tutorial
Published: 2022-02-23 21:13:54

A list with a few tips for images loading optimization. For you now and for me, somewhere in the future, when I forget about it.

First some [thematic tuning music](https://youtu.be/3shMD13Y2uU?t=14) 🤟.

And let's get started 🚀.

## Loading
The easiest way to speed up images loading is to add a [loading attribute](https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading) to your images:
- Images that don't have to be loaded immediatelly (not in a view, footer and etc.) should have `loading="lazy"`
- Those that need's be loaded early (are in the view or an LCP image) should have `loading="eager"`

If an image is marked as an [LCP](https://sortable.com/blog/ad-ops/what-is-largest-contentful-paint-lcp/) element, it should not be lazyloaded.

When an image is lazyloaded, make shure to prepare the space for that image so it doesn't cause unnecessary [layout shifts](https://web.dev/optimize-cls/). You can set the width and height attribute or use the [padding-top](https://css-tricks.com/preventing-content-reflow-from-lazy-loaded-images/) trick.

## Importance
The [importance attribute](https://developers.google.com/web/updates/2019/02/priority-hints) simply put says [when the downloading process should start](https://twitter.com/addyosmani/status/1087614725809160192?lang=cs). This can especially improve the LCP images loading. 
![Importance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cbs4npz7xtl36dj4qpu.png)

## Decoding
When an image have a [decoding="async"](https://twitter.com/addyosmani/status/938078402430382080) attribute, the rendering process doesn't pause when such image is found but it continues and the image is decoded asynchronously.
![Decoding](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o04xnld6eootkg1hwcq7.png) 

## Formats
When possible, use [AVIF](https://avif.io/blog/comparisons/avif-vs-webp/). Otherwise, at least webp. The compression is [far better](https://www.techspot.com/article/2299-image-file-formats/) then for ordinary jpg.
![Formats](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuy3obgwzfxsiwnyzpoq.png)
 

## Sizes
When loading large images, try to load different sizes from mobile to desktop. The sizes should differ around  200px on width or height. You can use the [picture element](https://developers.google.com/web/fundamentals/design-and-ux/responsive/images#art_direction_in_responsive_images_with_picture) for it.

## Preloads
It's a good practise to [preload](https://twitter.com/addyosmani/status/1283295232633659392) images that should be loaded as soon as possible.
But don't use it for every image on the page.
![Preloads](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iesowq7ghqleszkb2kty.png)

## Backgrounds
When you load some large background image that needs to be loaded as fast as possible [add a hidden image element](https://twitter.com/csswizardry/status/1276854595382325248) with the path to the background. It increases the priority and the image is loaded faster.
![Backgrounds](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6wckrgxg36gt3yl8miks.png)

Backgrounds that don't need to be loaded immediatelly should be lazyloaded. You can use the [intersection observer](https://blog.logrocket.com/lazy-loading-using-the-intersection-observer-api/) to figure out if the element with background is visible and then add a state class like `s-visible`. In the css then add the following:

```css
.image-background.s-loaded {
  /* 
    Image will be loaded only 
    if the element has the s-loaded class 
  */
}
```

## Icons
In case you have multiple small images (icons like images) that are used across the website, you can bundle them together using apps like [Icomoon App](https://icomoon.io/). This will decrease the amount of request as well as the total size.
![Icomoon App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lajcgn5k2qarl5gzvp1q.png)

## SVG
If the first image in the head is your logo and it is an SVG image add it directly into the page. This way it doesn't slow down the loading. 
However, there is also this way: 
1. Load it directly on the first request
2. When the page is loaded add a link element through javascript for that image into the page, so it loads on the backrground and cached
3. On another request load it through the img element.

## Share your tips!
If you have any other tips for images loading optimization, please share them in the comments. I will be happy to learn about them and it will definitely help other web developers that seeks information about how to improve the web performance 🙂.

-----

Stay in touch:
👉 [@8machy](https://twitter.com/8machy)
👉 [Stylify.dev](https://stylify.dev)
👉 [dev.to/machy8](dev.to/machy8)
👉 [medium.com/@8machy](medium.com/@8machy)