Skip to main content

Lazy Loading

Lazy loading is a performance optimization technique that delays the loading of non-critical resources, such as images, videos, or code, until they are needed. Instead of downloading everything at once, it loads what's necessary for the initial view first, and then loads other assets as a user scrolls down or interacts with the page. This approach significantly improves initial page load times and reduces bandwidth usage. 

This video explains the concept of lazy loading and how it reduces the initial bundle size:

How it works
  • Initial load: The browser loads only the critical content that is immediately visible to the user. For content below the fold, a placeholder or a lower-resolution image is often used.
  • On-demand loading: As the user scrolls down the page, the browser detects that new content is about to become visible and loads the corresponding images, videos, or other resources. 


Benefits of lazy loading

  • Faster initial page load: The page appears to load more quickly because it doesn't wait for all resources to download before displaying content.
  • Reduced bandwidth usage: Data is not consumed for resources that the user may never see.
  • Improved user experience: A faster load time and smoother performance lead to a better experience, especially for users on mobile devices or with slower internet connections.
  • Better SEO: Search engines favor websites that load quickly, so lazy loading can help improve search rankings. 

This video demonstrates how to implement lazy loading for images by using the 'loading' attribute:


Where it's used

  • Images and videos: The most common use case, where elements below the fold are loaded as the user scrolls.
  • Code splitting: In modern JavaScript applications, it can be used to load code for specific features only when the user navigates to them.
  • Databases: In a database context, it can mean loading large sets of data in smaller chunks as the user requests them