Home > Web Front-end > JS Tutorial > Let me share with you nine JavaScript image lazy loading libraries

Let me share with you nine JavaScript image lazy loading libraries

coldplay.xixi
Release: 2020-11-04 17:41:08
forward
3003 people have browsed it

javascriptThe column introduces several image lazy loading libraries.

Let me share with you nine JavaScript image lazy loading libraries

Related free learning recommendations: javascript (video)

Why do we need lazy loading of images

Performance is critical in web applications. You can have the most beautiful and attractive website in the world, but if it doesn’t load quickly on a browser, people will tend to skip it. Getting your website to perform really well can be quite tricky. This is because there are many bottlenecks in web development, such as expensive JavaScript, slow web font display, bulky image resources, etc.

In this article we mainly focus on the impact of image resources on the website. According to Jecelyn research, a webpage consumes an average of 5MB of data just to load images. This can be a heavy burden for users as mobile data is very expensive in some countries. Users also experience issues with sites taking too long to load, especially on slow connections. These can have a negative impact on your website.

According to Jakob Nielson research, here are some important statistics you should keep in mind:

  • Website load times below 100 milliseconds are considered instantaneous.
  • Latency between 100 and 300 milliseconds is perceptible.
  • 47% of consumers expect web pages to load in two seconds or less.
  • 40% of consumers will wait no more than 3 seconds to load before abandoning a website.

What is lazy loading?

There are several strategies that can provide efficient services for the image resources of the website without affecting performance and quality, and lazy loading is one of them. Lazy loading means loading only what is needed and deferring the rest until needed. This strategy can be applied to images, videos, text, and other types of data. But mostly, it's suitable for bulky content like image assets.

There are several ways to implement lazy loading of images on your website. For example, you can use the Intersection Observer API, or use an event handler to determine whether an element is in view. There are also several powerful JavaScript libraries. You can use the following image lazy loading library methods according to your needs and compatibility. Let’s take a look!

Lazy Sizes

Lazy Sizes is one of the best lazy loading libraries currently, with more than 14.1K collections on Github, and only 3.4kB after compression. It also supports approximately 98.5% of browser users, and its documentation is written in an easy-to-understand manner.

Features

  • Contains support for responsive images.
  • Optimize SEO by detecting search engines with the help of user agents and loading all images instantly.
  • Based on efficient and practical code.
  • Preload resources when the network connection is idle.
  • Contains support for LQIPs.
  • Supports IntersectionObserver, MutationObserver and getElementsByClassName etc.
  • Supports the use of plug-ins to extend features.
  • Supports automatic calculation of response image size.

You can see examples here.

Lozad.js

Lozad.js supports lazy loading of images, iframe, ads, videos and other elements. It has nearly 6.4K collections on Github and is very popular in the community. According to the research team, this library is used by web applications of several brands, including Tesla, Domino, Xiaomi, and the BBC. It's very small, only 1.1kB gzipped. Since it uses the IntersectionObserver API and the MutationObserver API, it supports around 92% of browser users.

Features

  • There are no dependencies.
  • Support lazy loading of dynamically added elements.
  • Uses entirely JavaScript.
  • Contains support for LQIPs and response images.
  • More efficient than libraries using getBoundingClientRect().
  • Polyfills can be used on unsupported browsers.

You can see examples here.

Tuupola’s Lazyload

Tuupola’s Lazyload is another popular image lazy loading library on Github, with nearly 8.4K collections. It uses the IntersectionObserver API and is simple and easy to use. It is only 956 bytes after compression, which is smaller than other libraries. This can be attributed to the fact that it only uses the IntersectionObserver API, as other libraries use other combinations to achieve better compatibility and performance. Furthermore, thanks to this, 92% of browser users currently support it.

Features

  • It includes a jQuery wrapper for convenience.
  • Includes support for LQIPs and response images.
  • The core IntersectionObserver API can be configured by passing additional parameters.

Vanilla Lazyload by Andrea Verlicchi

Vanilla lazy load is another pure JavaScript library for lazy loading of images, videos and iframes. It is very popular on Github, with nearly 1500 repositories and packages available. It has over 1.9 million downloads per year on NPM. It's only 2.7kB compressed. Similar to other libraries, this library uses the IntersectionObserver API, which is supported by 92% of browser users.

  • SEO friendly as the library does not cover images from search engines.
  • Supports unstable network connections as the library will automatically reload images after the connection is interrupted.
  • If the image exits the viewport, cancel loading of the image.
  • Contains support for LQIPs and response images.
  • Uses entirely JavaScript.

#You can see an example here.

Yall.js

Yall.js is another JavaScript library that also only uses the IntersectionObserver API to lazy load images, videos, iframe and CSS background image. This library has approximately 1.1K collections and is used by 91 users in their project libraries. This library can be compressed to 1kB. As we saw with previous libraries, Yall.js also supports 92% of browser users because of the use of the IntersectionObserver API. It is important to note that if the browser does not support the IntersectionObserver API, there will be no backup. In that case you must use polyfill.

Features

  • Supports the detection of dynamically loaded elements with the help of MutationObserver API.
  • Use the requestIdleCallback method to optimize browser idle time.
  • Supports direct implementation of LQIP through the src attribute.
  • Support lazy loading of CSS background.

Layzr.js

Layzr.js is a lightweight image lazy loading library based on JavaScript. It mainly uses Element.classList, with few ES5 array methods and requestAnimationFrame methods. Thanks to these APIs, more than 97% of browser users support this library. Layzr.js has more than 5.6K collections on Github and is very popular. It is only 1kB after compression.

  • There are no dependencies.
  • Intelligent selection of image sources based on browser compatibility and availability.
  • Support dynamically added elements.
  • Clear and concise documentation and examples.
  • Viewport adjustment images with threshold properties are lazy loaded, allowing images to be loaded earlier or later as needed.

You can see examples here.

Blazy.js

Blazy.js is another lightweight JavaScript lazy loading library that can handle images, videos and iframe. It is very popular on Github with 2.6K collections and is currently used by over 860 open source project libraries. It's only 1.9kB compressed.

Use the Element.getBoundingClientRect() method, which may not perform as well as other libraries implementing the IntersectionObserver API. But due to this approach, this library is supported by more than 98% of browser users. It also uses Element.closest(). Browser support for this API is just over 94%. In this case, you don't have to worry about the missing 6% because the library contains a polyfill for unsupported browsers.

Features

  • Used on real websites with millions of monthly visits.
  • There are no dependencies.
  • Support responsive images.
  • Similar to Layzr.js allows loading elements with offsets.
  • Clear documentation with example code.
  • Supports module formats such as AMD, CommonJS and globals.
  • Very easy to provide retina pictures.

You can see examples here.

Responsively Lazy

Responsively lazy is also a lazy loading library for images. Its content is concise, only 1.1kB compressed. Because of its good syntax implementation, it stands out from many libraries. Most of the libraries we discussed above require you to use the noscript tag for browsers that disable javascript, ignore the src attribute, etc. But lazy can use the traditional src attribute and add srcset and src attributes for supported browsers. This makes this library search engine optimization (SEO) friendly. This library also uses Element.getBoundingClientRect() so forced layout reflows will also occur in this library.

In addition, this library has nearly 1.1K collections on Github, and almost 95% of browser users support this library.

Features

  • Support responsive images.
  • Support webp.
  • Search Engine Optimization (SEO) friendly.
  • Not many customizations available.

You can see examples here.

LazyestLoad.js

LazyestLoad.js is one of the smallest libraries on this list. It's only 700 bytes, compressed to just 639 bytes. There are two versions of this library, lazyload and lazyestload. They all have different uses, the lazyload version works like the normal library, the image will load when it is about to enter the viewport; however the lazyestload version only loads when the user stops scrolling and the image The image will only load if it is in the viewport or within 100 pixels. This helps reduce network load if the user is just scrolling without pausing to see the image.

It mainly uses the Element.getBoundingClientRect() method, which is not efficient compared to other implementations and is also known to trigger layout rearrangements.

This library only handles images, unlike other libraries that can handle videos and iframe. It also has over 15,000 collections on Github.

Features

  • Simple and straightforward.
  • Does not allow extensive customization like other libraries.
  • Support responsive images.
  • The documentation is not detailed enough.

You can view the lazyload example and view the lazyestload example.


As most modern browsers will support native lazy loading, it is recommended to use native implementations. Native lazy loading also ensures that images load lazily even if JavaScript is disabled in the browser. Just use the loading="lazy" attribute in the img tag and save yourself all the trouble.

Most modern browsers support native lazy loading, and Safari will soon support it. Currently, the browser support rate is 74%. If the browser does not support the native implementation, you can use polyfill or one of the above lazy loading libraries.

To be safe, you may still need to use dynamic imports to implement one of the libraries.

Know your target audience

If you carefully analyze all the above given libraries, you will find that they compete fiercely in three aspects: performance, size and browser compatibility ( user coverage). These usually have to sacrifice at least one to improve the level of the other.

For example, if you use a library that implements the IntersectionObserver API, you will get a high-performance library, but its user coverage will be smaller. If patching is required, there needs to be a fallback option, such as polyfills, which will increase the overall size of the library.

In another example, if the lazy loading library uses the getBoundingClientRect() method, it will not perform as well as the IntersectionObserver API because it is known to have forced layout reflow issues . Although performance is sacrificed, user coverage will be higher than the former. Hope I can make this clear.

How to minimize compatibility issues and maximize performance?

These areas can be improved by understanding your target audience and their browser usage. If you know your target audience and the browsers they use, you can ensure that your implementation of lazy loading is better suited for those browser versions. This will reduce the need to include polyfill for unsupported browsers since it will already be known which browsers need to be concerned. When you have an outlier (unsupported browser), images can load directly without any lag or delay. If you have a good understanding of your audience, the number of these outliers will be negligible.

This approach will help to use an implementation library that performs well, keeps the library size to a minimum by ignoring browser exceptions, and supports the target user's browser version.


This article briefly discusses JavaScript’s lazy loading library and some methods to improve efficiency and user experience. Have your say in the comments below.

Thanks for reading and happy coding! !

The above is the detailed content of Let me share with you nine JavaScript image lazy loading libraries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template