
Web Performance Optimization: The Ultimate Cheat Sheet
Web Performance Optimization: The Ultimate Cheat Sheet π
Web performance isn't just about speedβit's about delivering exceptional user experiences that keep visitors engaged and improve your site's search rankings. With Google's Core Web Vitals becoming crucial ranking factors, understanding performance optimization has never been more important.
Whether you're a frontend developer looking to optimize load times or a web professional aiming to improve SEO rankings, this comprehensive guide covers the essential techniques and metrics you need to know.
In this guide, you'll learn:
- π How to optimize resource loading with
preload
,defer
, andasync
- π Understanding Core Web Vitals: CLS, LCP, and TTI
- β‘ Best practices for speed and user experience optimization
- π― When to use each optimization technique
Resource Loading Optimization
Preload: Start Early, Render Fast β‘
preload
instructs the browser to start downloading critical resources earlyβbefore they're actually needed in the parsing process.
How It Works
- Does it execute? β Noβresources must be referenced later (e.g., in
<script>
,<img>
, etc.) - When to use: Load JS bundles, fonts, or images early for faster rendering
<!-- Preload critical JavaScript bundle -->
<link rel="preload" href="/main.js" as="script" />
<!-- Preload essential fonts -->
<link rel="preload" href="/fonts/primary.woff2" as="font" type="font/woff2" crossorigin />
<!-- Preload hero images -->
<link rel="preload" href="/hero-image.jpg" as="image" />
Defer: Parse First, Execute Later π
defer
loads JavaScript in parallel with HTML parsing but waits to execute until the DOM is fully constructed.
Key Characteristics
- Blocks parsing? β No
- Executes in order? β Yes (maintains script order)
- Best for: Application scripts that depend on the DOM (React, Vue, etc.)
<!-- Perfect for framework scripts -->
<script src="/react.js" defer></script>
<script src="/app.js" defer></script>
<!-- React loads first, then app.js -->
Async: Load and Execute Immediately πββοΈ
async
loads JavaScript in parallel and executes immediately when ready, without waiting for DOM parsing to complete.
Key Characteristics
- Blocks parsing? β No (during load)
- Executes in order? β No (executes when ready)
- Best for: Independent scripts like analytics, ads, or tracking
<!-- Perfect for analytics that don't depend on DOM -->
<script src="https://analytics.com/track.js" async></script>
<script src="https://ads.com/display.js" async></script>
Core Web Vitals: Measuring User Experience
Cumulative Layout Shift (CLS) π
CLS measures visual stability by tracking unexpected layout movements during page load.
What Causes Bad CLS?
- Images without dimensions
- Fonts that load late and change text size
- Dynamic content insertion
How to Fix CLS Issues
<!-- β Bad: No dimensions specified -->
<img src="/hero.jpg" alt="Hero image" />
<!-- β
Good: Reserve space with dimensions -->
<img src="/hero.jpg" alt="Hero image" width="800" height="400" />
<!-- β
Better: Use aspect-ratio in CSS -->
<style>
.hero-image {
aspect-ratio: 16/9;
width: 100%;
}
</style>
π― Goal: CLS score < 0.1
Largest Contentful Paint (LCP) πΌοΈ
LCP measures loading performance by tracking when the largest visible element becomes fully rendered.
Common LCP Elements
- Hero images
- Large text blocks
- Video posters
LCP Optimization Strategies
<!-- Optimize hero images -->
<link rel="preload" href="/hero.jpg" as="image" />
<img src="/hero.jpg" alt="Hero" loading="eager" />
<!-- Use modern image formats -->
<picture>
<source srcset="/hero.webp" type="image/webp" />
<source srcset="/hero.avif" type="image/avif" />
<img src="/hero.jpg" alt="Hero" />
</picture>
π― Goal: LCP < 2.5s
Time to Interactive (TTI) β‘
TTI measures interactivity by tracking when the page becomes fully responsive to user input.
Common TTI Blockers
- Heavy JavaScript execution
- Long-running tasks on the main thread
- Poor script loading strategy
TTI Optimization Techniques
// Code splitting for better TTI
const HeavyComponent = lazy(() => import('./HeavyComponent'));
// Use requestIdleCallback for non-critical tasks
function performNonCriticalTask() {
if ('requestIdleCallback' in window) {
requestIdleCallback(() => {
// Run heavy computation during idle time
processLargeDataset();
});
} else {
// Fallback for older browsers
setTimeout(processLargeDataset, 1);
}
}
π― Goal: TTI < 3.8s
Performance Optimization Strategies
Resource Prioritization Matrix
Resource Type | Strategy | Why |
---|---|---|
Critical CSS | Inline or preload | Prevents render blocking |
Web fonts | Preload + font-display | Reduces layout shift |
Hero images | Preload | Improves LCP |
Framework JS | Defer | Maintains execution order |
Analytics | Async | Non-blocking, independent |
Advanced Optimization Techniques
Resource Hints for Better Performance
<!-- DNS prefetch for external domains -->
<link rel="dns-prefetch" href="//fonts.googleapis.com" />
<!-- Preconnect for critical third-party resources -->
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<!-- Prefetch for likely next page navigation -->
<link rel="prefetch" href="/about-page.html" />
Critical Path Optimization
<!DOCTYPE html>
<html>
<head>
<!-- Critical CSS inlined -->
<style>
/* Above-the-fold styles here */
</style>
<!-- Preload critical resources -->
<link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin />
<link rel="preload" href="/hero.jpg" as="image" />
<!-- Non-critical CSS -->
<link rel="stylesheet" href="/styles.css" media="print" onload="this.media='all'" />
</head>
<body>
<!-- Content -->
<!-- Deferred scripts -->
<script src="/app.js" defer></script>
</body>
</html>
Common Performance Pitfalls
Mistakes to Avoid β
β Loading everything synchronously (blocks rendering)
β Not specifying image dimensions (causes layout shift)
β Using async for dependent scripts (breaks execution order)
β Ignoring font loading strategies (causes invisible text)
Best Practices β
β
Use preload for critical resources (fonts, hero images)
β
Defer application JavaScript (maintain DOM dependency)
β
Async for independent scripts (analytics, social widgets)
β
Monitor Core Web Vitals regularly (use tools like PageSpeed Insights)
Performance Monitoring Tools
Essential Tools for Measurement
- Lighthouse: Built into Chrome DevTools
- PageSpeed Insights: Google's web performance analyzer
- WebPageTest: Detailed performance testing
- Chrome DevTools: Real-time performance profiling
Setting Performance Budgets
// Example performance budget
const performanceBudget = {
'first-contentful-paint': 1500, // ms
'largest-contentful-paint': 2500, // ms
'cumulative-layout-shift': 0.1, // score
'time-to-interactive': 3800, // ms
'total-bundle-size': 250000, // bytes
};
Conclusion
Web performance optimization is a continuous journey that directly impacts user experience, SEO rankings, and business metrics. By mastering resource loading strategies and understanding Core Web Vitals, you can build blazing-fast web applications that delight users.
Key Takeaways π‘
- π Use preload for critical resources that need to load early
- π Defer application scripts to maintain DOM dependencies
- πββοΈ Async independent scripts like analytics and tracking
- π Monitor Core Web Vitals regularly and set performance budgets
- β‘ Optimize the critical rendering path for faster perceived performance
Quick Reference Card π
Goal | Strategy | Implementation |
---|---|---|
Load assets early | preload | <link rel="preload" href="/critical.js" as="script"> |
Don't block rendering | defer , async | <script src="/app.js" defer></script> |
Fast content appearance | Optimize LCP | Preload hero images, optimize fonts |
Quick interactivity | Optimize TTI | Code splitting, reduce JavaScript |
Stable layout | Minimize CLS | Set image dimensions, font strategies |
Start implementing these techniques in your next project and watch your performance scores soar! πβ¨
Which Core Web Vital will you optimize first?