Cloudinary Blog

Lazy Loading for Optimal Performance

By Ezequiel Bruni
Lazy Loading for Performance

Who doesn't love some striking imagery to drive your point home? Whether you're selling a product or service, trying to communicate complex ideas, or simply captivate the emotions of your users, pictures can do that. Everyone knows they work, and everyone loves them.

Well, everyone except the actual web servers. Thankfully, they have not yet gained sentience, so we don't have to worry about their feelings. Even so, there is a cost to having images take up around 70% of all bandwidth.

It costs us in terms of storage space, sure. More importantly, it cost both servers and users in terms of bandwidth and data caps.

A long time ago (in Internet Years) browser developers figured out they could load and render pages faster if they started loading more than one external resource at a time. So they did. Now, while scripts and CSS are still downloading, your browser will usually try to grab all of the images, too.

That way, in theory, it's all ready to go as soon as the CSS and JS tell the browser what to do with all of those images. Well, it's ready to go sooner, in any case.

But what if the user doesn't scroll all the way down the page? What if they never see many or most of those images? That's wasted data on both ends.

This is where we turn to lazy loading.

How this works

For the uninitiated, lazy loading is simply waiting to load the images until the user gets to them. Only the necessary images are ever loaded, saving potential gigabytes upon gigabytes of bandwidth. The more users your site has, the more you save.

Don't take it from me. Take it from Chris Coyier of CSS-Tricks fame.

On a high traffic site, say 2 million of 5 million users visit a blog post with a lot of images on it, but never scroll down. Below the fold, there is 750k of images. That’s going to save you a boatload of bandwidth (1.5 million megabytes…)

But even that is just a quote, though it comes from a pretty smart guy. If you want a real-world example, look at this post-mortem by NYStudio+107. They used lazy loading (and a few other important techniques, but we're focused on lazy loading right now) to drop from a page load time of 107.8 seconds to 2.8 seconds.

It's not a proof-of-concept either. It's a real-world site they built. Incidentally, the blog post itself uses lazy-loading, so there's another example.

Ok, that's awesome! Let's do it!

Okay, but there are a few things to think about:

  • This is only useful if you have a lot of images below the fold. If you just have a hero image and then a bunch of text... it's not worth it. Consider your content.
  • Lazy loading is not a built-in browser feature. It has to be done with JavaScript. Adding lazy loading will increase the overall complexity of any project. This will cost in terms of development time and testing.
  • Doing it wrong may result in users not seeing images at all.

Now, if you have all of that covered, and you still want to do this, here are a couple of tips:

  •  Load images just before they enter the viewport whenever possible. It won't always work, but it's the ideal.
  • Since the above tip won't always work, you're going to need to reserve space for the images. If you don't, you may see the rest of the layout jumping around, and that's never cool.
  • You can use background colors, loading animations, or even tiny image previews to let users know that something will be loading in that extra space.
  • Have a fallback solution. Always have a fallback solution.

Again, it would be better if they never saw it, but these things happen.

Tools

We would never end an article like this without telling you where to start. The library I'd recommend is called lazysizes. It supports both responsive and normal images, and can also work with other elements like iframes.

Even better, it can automatically generate the appropriate "sizes" attribute for your responsive images on the fly. So you'll never have to set those manually again.

It's designed to be fast, extendable, and play nice with other JS libraries. It's also designed to never hide content from search engines, so it shouldn't impact your SEO.

Conclusion

It's time to get lazy, people! (I couldn't resist.)

The only real downside is the JavaScript dependency. In the future, you may be able to skip the JS, once the IntersectionObserver API is implemented in all browsers. Right now, only Chrome and Opera support it fully, and out of the box.

That small issue aside, this technique could save you, and your site, a lot of data. Data is money, for both you and your users.

Ezequiel Bruni Ezequiel Bruni is a web/UX designer, writer, and aspiring e-sports commentator. When he's not up to his finely-chiseled ears in wire-frames and front-end code, or ranting about the same, he indulges in video games, beer, pizza, video games, fantasy novels, stand-up comedy, and video games..

 

Recent Blog Posts

Generate Waveform Images from Audio with Cloudinary

This is a reposting of an article written by David Walsh. Check out his blog HERE!
I've been working a lot with visualizations lately, which is a far cry from your normal webpage element interaction coding; you need advanced geometry knowledge, render and performance knowledge, and much more. It's been a great learning experience but it can be challenging and isn't always an interest of all web developers. That's why we use apps and services specializing in complex tasks like Cloudinary: we need it done quickly and by a tool written by an expert.

Read more
Make All Images on Your Website Responsive in 3 Easy Steps

Images are crucial to website performance, but most still don't implement responsive images. It’s not just about fitting an image on the screen, but also making the the image size relatively rational to the device. The srcset and sizes options, which are your best hit are hard to implement. Cloudinary provides an easier way, which we will discuss in this article.

Read more

The Future of Audio and Video on the Web

By Prosper Otemuyiwa
The Future of Audio and Video on the Web

Web sites and platforms are becoming increasingly media-rich. Today, approximately 62 percent of internet traffic is made up of images, with audio and video constituting a growing percentage of the bytes.

Read more

Embed Images in Email Campaigns at Scale

By Sourav Kundu
Embed Images in Email Campaigns at Scale

tl;dr

Cloudinary is a powerful image hosting solution for email marketing campaigns of any size. With features such as advanced image optimization and on-the-fly image transformation, backed by a global CDN, Cloudinary provides the base for a seamless user experience in your email campaigns leading to increased conversion and performance.

Read more
Build the Back-End For Your Own Instagram-style App with Cloudinary

Github Repo

Managing media files (processing, storage and manipulation) is one of the biggest challenges we encounter as practical developers. These challenges include:

A great service called Cloudinary can help us overcome many of these challenges. Together with Cloudinary, let's work on solutions to these challenges and hopefully have a simpler mental model towards media management.

Read more

Build A Miniflix in 10 Minutes

By Prosper Otemuyiwa
Build A Miniflix in 10 Minutes

Developers are constantly faced with challenges of building complex products every single day. And there are constraints on the time needed to build out the features of these products.

Engineering and Product managers want to beat deadlines for projects daily. CEOs want to roll out new products as fast as possible. Entrepreneurs need their MVPs like yesterday. With this in mind, what should developers do?

Read more