May we use cookies so Meta, Google, LinkedIn and ChatGPT see your visit and we learn which ads work? PrivacyMay we use cookies to see which pages help, and let Meta, Google, LinkedIn and ChatGPT see your visit, so we know which of our ads work? That way we spend less on ads that do not work. We do not sell your data, and the site works the same if you decline. You can change your answer at any time under Cookie settings at the foot of the page. Privacy policy
Loading...
System Design Masterclass · Web Content Delivery
Free preview: the first 2 parts of this 7-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$35 once, by card or PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$35 once, by card or PayPal
See the System Design coursein Web Content Delivery
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
A blog homepage displayed 50 article thumbnails. All 50 loaded on page load, regardless of scroll position. Analytics showed 70% of visitors scrolled past only 5-6 articles before leaving. Those visitors downloaded 44 images they never saw. 3MB of wasted per visit. On mobile, initial page load took 4 seconds because the browser was busy fetching images below the fold.
Adding loading="lazy" to the below-fold images cut initial page weight from 3.5MB to 400KB. First Contentful Paint dropped from 4 seconds to 1.1 seconds. The images below the fold loaded silently as users scrolled, completely invisible to them.
Lazy loading is a simple trade: defer downloading resources that are not visible yet. The initial paint is faster because the browser focuses its bandwidth on what the user can actually see. Resources that are never scrolled to are never downloaded at all.
Native lazy loading (HTML attribute):
<img src="photo.jpg" loading="lazy" alt="Product photo" width="400" height="300">
One attribute. No JavaScript. The browser handles everything: it monitors the viewport, starts fetching the image when it is roughly 1250px from the visible area (the exact threshold varies by browser and connection speed), and renders it when the data arrives.
Supported on 96%+ of browsers as of 2024. The only notable gap is very old Safari versions (pre-15.4).
JavaScript IntersectionObserver (custom behavior):
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
observer.unobserve(img);
}
});
}, { rootMargin: '200px' });
document.querySelectorAll('img[data-src]').forEach(img => observer.observe(img));
IntersectionObserver gives you control over the trigger distance (rootMargin), lets you lazy-load non-image content (iframes, video, heavy components), and works with custom placeholder animations. But for simple image lazy loading, the native attribute is simpler and more reliable.