USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Performance Optimization

Lesson 6/30 | Study Time: 28 Min

Performance Optimization (Lazy Loading, Preload) focuses on improving website speed and efficiency by controlling when and how resources are loaded.

Lazy loading delays non-critical content until it is needed, reducing initial load time, while preload prioritizes important resources so they are available as soon as possible.

Together, these techniques enhance user experience, save bandwidth, and boost overall website performance.

Understanding Web Performance Metrics

Web performance isn't just "fast"—it's measurable through industry-standard metrics that guide optimization.

Core Web Vitals from Google set concrete targets browsers now report automatically.

Lazy loading and preload target specific vitals like Largest Contentful Paint (LCP) and First Input Delay (FID).

These techniques work at the HTML level before CSS/JS even loads, making them foundational. They ensure critical content renders first while deferring non-essential assets.

Core Web Vitals 

Google's trio of metrics powers search rankings and ad platforms:



ProTip: Test your sites at PageSpeed Insights before/after implementing these.

Lazy Loading: Defer Non-Critical Assets xml

Lazy loading tells browsers to load images/videos only when they enter the viewport, slashing initial page weight by 50-80% on image-heavy sites.

Native HTML5 support (since 2020) eliminates JavaScript libraries like lazysizes. It's automatic across Chrome, Firefox, Safari, and Edge.

This technique shines for galleries, portfolios, and e-commerce—perfect for your course projects.

Implementing Native Lazy Loading

Add loading="lazy" to <img> and <iframe> tags—no JS required.


Basic Example:

<!-- Hero image loads immediately -->
<img src="hero.jpg" alt="Hero" width="1200" height="600">

<!-- Gallery images load on scroll -->
<img src="gallery1.jpg" alt="Project 1" loading="lazy" width="400" height="300">
<iframe src="youtube-video" loading="lazy" title="Demo"></iframe>


Browser Behavior:


Critical above-fold content loads normally.

Browser predicts viewport using scroll position.

Assets load ~200px before entering view.


Advanced Lazy Loading Strategies

Combine with responsive images for maximum impact:


1. Use sizes attribute for responsive estimation:

xml
<img src="small.jpg"
srcset="small.jpg 400w, large.jpg 1200w"
sizes="(max-width: 600px) 100vw, 50vw"
loading="lazy"
alt="Responsive image">


2. Picture element with lazy:

xml
<picture>
<source media="(min-width: 768px)" srcset="large.webp">
<img src="fallback.jpg" loading="lazy" alt="Adaptive image">
</picture>


3. Fallback for Older Browsers:

xml
<img src="hero.jpg" alt="Hero"
loading="lazy"
onerror="this.src='placeholder.jpg'">


Preload: Prioritize Critical Resources

Preload fetches high-priority assets (fonts, hero images, critical CSS/JS) early in the Critical Rendering Path.

Use <link rel="preload"> in <head> to hint browsers about render-blocking resources. Supported since 2016, it's now standard across all modern browsers.

Unlike rel="stylesheet" (which blocks rendering), preload fetches without blocking HTML parsing.


Preload Resource Types

Target these common bottlenecks:



Syntax

xml
<head>
<!-- Critical font -->
<link rel="preload" href="fonts/modern.woff2" as="font" type="font/woff2" crossorigin>

<!-- Hero image -->
<link rel="preload" href="hero.jpg" as="image">

<!-- Critical CSS -->
<link rel="preload" href="critical.css" as="style">
</head>

Crossorigin for Fonts: Required for security when fonts load from same-origin.


Strategic Preloading Workflow

Follow this prioritized checklist:


1. Identify critical resources using Chrome Coverage tab (F12 → Coverage).

2. Add preload links ordered by render priority.

3. Combine with rel="preconnect" for third-party domains:

xml
<!-- Google Fonts optimization -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700" as="style">

<!-- YouTube embeds -->
<link rel="preconnect" href="https://www.youtube.com">


Real-World Portfolio Example:

xml
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jane Doe - Portfolio</title>

<!-- Performance boosters -->
<link rel="preload" href="fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="hero.jpg" as="image">
<link rel="preload" href="critical.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
</head>

Preload vs Prefetch Comparison


Pro Tip: Use onload="this.rel='stylesheet'" for CSS preload to avoid flash of unstyled content.


Combining Lazy Loading + Preload


Maximum performance requires both techniques working together:

text
Hero image: preload (immediate)
Fonts: preload (immediate)
Gallery images: lazy (on scroll)
Below-fold videos: lazy + muted autoplay


Complete Optimized Page Template

xml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Optimized Portfolio</title>

<!-- Preloads -->
<link rel="preload" href="fonts/modern.woff2" as="font" crossorigin>
<link rel="preload" href="hero.avif" as="image">
</head>
<body>
<img src="hero.avif" alt="Hero" width="1200" height="600">

<section class="gallery">
<img src="project1.webp" loading="lazy" alt="Project 1" width="400" height="300">
<!-- More lazy images -->
</section>
</body>
</html>


Performance Gains (real-world tests):


1. LCP: 1.2s → 0.8s (-33%)

2. Total blocking time: 400ms → 120ms (-70%)

3. Page weight: 3.2MB → 1.1MB (-66%)


Testing and Validation Tools


1. Chrome DevTools (Performance Tab):

Chrome DevTools allows you to record and analyze page load and runtime performance directly in the browser.

The Performance tab visualizes network activity, scripting, rendering, and layout shifts, helping identify bottlenecks across different devices.

2. Lighthouse:

Lighthouse is an automated auditing tool that generates detailed reports on performance, accessibility, SEO, and best practices.

It can be run from Chrome DevTools or via the CLI using npx lighthouse for repeatable testing.

3. WebPageTest.org:

WebPageTest enables testing from multiple geographic locations, browsers, and network conditions.

It provides deep insights such as load timelines, waterfalls, and visual progress to simulate real-world user experiences.

4. Real User Monitoring (Core Web Vitals in Google Analytics):

Real User Monitoring tracks actual user interactions and performance metrics like LCP, CLS, and INP.

Google Analytics Core Web Vitals reports help measure real-world performance and guide data-driven optimizations.


Common Pitfalls: