Web Performance Optimization: Core Web Vitals and Beyond
Performance14 min read

Web Performance Optimization: Core Web Vitals and Beyond

Master web performance optimization techniques. Learn about Core Web Vitals, lazy loading, code splitting, caching strategies, and more.

Taha Kocal

Taha Kocal

Full Stack Developer

Nov 28, 2024
#Performance#Core Web Vitals#Optimization#SEO#Frontend

Web performance optimization is the practice of making pages load and respond faster by improving metrics like Google's Core Web Vitals: Largest Contentful Paint (LCP), which should stay under 2.5 seconds, Cumulative Layout Shift (CLS), which should stay below 0.1, and interaction responsiveness. Performance directly affects both user experience and SEO, since Google uses Core Web Vitals as a ranking factor, and slower pages measurably increase bounce rates and reduce conversions. The highest-leverage techniques are optimizing the largest above-the-fold element with preloading and modern image formats, lazy loading offscreen images and components, splitting JavaScript bundles by route, reserving space for images and ads to prevent layout shift, and caching aggressively with service workers and CDN headers. This guide explains each Core Web Vital, then walks through lazy loading, code splitting, caching strategies, and real-user performance monitoring with practical code.

What Are Core Web Vitals?

Core Web Vitals are three key metrics Google uses to measure user experience. Understanding and optimizing these metrics is essential for modern web development.

LCP (Largest Contentful Paint)

Measures how long it takes for the largest content element to render. Good: < 2.5s, Needs Improvement: 2.5-4s, Poor: > 4s.

LCP optimization strategies:

  • Optimize and compress images
  • Preload critical resources
  • Use CDN for static assets
  • Inline critical CSS
  • Remove render-blocking resources
html
<!-- Optimized image loading -->
<img
  src="hero.webp"
  srcset="hero-480.webp 480w, hero-800.webp 800w, hero-1200.webp 1200w"
  sizes="(max-width: 600px) 480px, 800px"
  loading="eager"
  fetchpriority="high"
  alt="Hero image"
/>

<!-- Font preloading -->
<link rel="preload" href="/fonts/custom.woff2" as="font" type="font/woff2" crossorigin />

INP (Interaction to Next Paint)

Measures how quickly the page responds to user interactions. Good: < 200ms, Needs Improvement: 200-500ms, Poor: > 500ms.

INP optimization strategies:

  • Break up long JavaScript tasks
  • Use web workers for heavy computations
  • Debounce/throttle event handlers
  • Minimize main thread work
  • Use requestAnimationFrame for visual updates

CLS (Cumulative Layout Shift)

Measures visual stability during page load. Good: < 0.1, Needs Improvement: 0.1-0.25, Poor: > 0.25.

tsx
// Always specify image dimensions
<img src="photo.jpg" width={800} height={600} alt="Photo" />

// Use aspect ratio containers
<div className="aspect-video">
  <iframe src="..." />
</div>

// Skeleton loading prevents layout shift
function ProductCard({ isLoading, product }) {
  if (isLoading) {
    return (
      <div className="animate-pulse">
        <div className="h-48 bg-gray-300 rounded" />
        <div className="h-4 bg-gray-300 rounded mt-2 w-3/4" />
      </div>
    );
  }
  return <div>{/* actual content */}</div>;
}

How Does Lazy Loading Work?

tsx
// Native lazy loading
<img src="photo.jpg" loading="lazy" alt="Photo" />

// React component lazy loading
const HeavyComponent = lazy(() => import('./HeavyComponent'));

function App() {
  return (
    <Suspense fallback={<Loading />}>
      <HeavyComponent />
    </Suspense>
  );
}

Code Splitting

javascript
// vite.config.js - Manual chunks
export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['react', 'react-dom'],
          animations: ['framer-motion', 'gsap'],
        },
      },
    },
  },
});

Caching Strategies

javascript
// Service Worker caching
const CACHE_NAME = 'v1';

self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request).then((response) => {
      return response || fetch(event.request);
    })
  );
});

How Do You Monitor Performance?

typescript
import { onCLS, onINP, onLCP } from 'web-vitals';

function sendToAnalytics({ name, delta, id }) {
  gtag('event', name, {
    event_category: 'Web Vitals',
    value: Math.round(name === 'CLS' ? delta * 1000 : delta),
    event_label: id,
  });
}

onCLS(sendToAnalytics);
onINP(sendToAnalytics);
onLCP(sendToAnalytics);

Performance is not just about speed—it's about user experience, accessibility, and business success. Every 100ms of improvement can increase conversions.

Web performance optimization is an ongoing process. Regular measurement, monitoring, and improvement are key to maintaining a fast, user-friendly website.

Share this article

Related Articles