Home About Skills Products Work
Projects Services Experience
Learn
Tutorials Courses Blogs Resources
Contact
Blog · JavaScript

Web Performance Optimization: Understanding Core Web Vitals

Web Performance Optimization: Understanding Core Web Vitals
Advertisement

Core Web Vitals are Google's specific, measurable metrics for real-world page experience — and they directly affect both user experience and search ranking. Here's what each one actually measures and the concrete fixes that move the needle.

LCP: Largest Contentful Paint

LCP measures how long it takes the largest visible element (usually a hero image or heading) to render. Target: under 2.5 seconds.

<!-- Preload the hero image instead of letting the browser discover it late -->
<link rel="preload" as="image" href="/hero.webp" fetchpriority="high">

The most common LCP killer is a large, unoptimized hero image loaded without priority hints — the browser doesn't know it's the most important thing on the page until it's already parsed most of the HTML.

INP: Interaction to Next Paint

INP (which replaced First Input Delay in 2024) measures how long the page takes to visibly respond after a click, tap, or keypress. Target: under 200ms.

// Bad: one giant synchronous task blocks the main thread
processHugeArray(data);

// Better: break work into chunks, yielding back to the browser between them
async function processInChunks(data, chunkSize = 100) {
  for (let i = 0; i < data.length; i += chunkSize) {
    processChunk(data.slice(i, i + chunkSize));
    await new Promise(r => setTimeout(r, 0)); // yield to the browser
  }
}

Long JavaScript tasks (anything over ~50ms) block the main thread — the page LOOKS frozen because it genuinely can't process the next click until the current task finishes.

CLS: Cumulative Layout Shift

CLS measures unexpected visual movement — content jumping around as the page loads. Target: under 0.1.

<!-- Reserve space before the image loads, so nothing shifts once it does -->
<img src="/photo.jpg" width="800" height="450" alt="">

Explicit width/height attributes let the browser reserve the correct aspect-ratio space immediately, before the image file has even downloaded — the #1 fix for CLS caused by images.

Measuring in the Real World

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

onLCP(console.log);
onINP(console.log);
onCLS(console.log);

Lab tools (Lighthouse) simulate one device on one network condition. Real User Monitoring (via web-vitals reporting to your own analytics) shows what actual visitors on actual devices and connections experience — often meaningfully worse than a Lighthouse score run on a fast office connection.

Why This Actually Matters

Beyond SEO ranking, these three metrics correlate directly with real business outcomes — studies across major ecommerce sites consistently show measurable drops in conversion rate for every extra second of load time. Fast isn't just a nice-to-have; it's directly tied to revenue.

JavaScript Fundamentals Every Beginner Should Master

JavaScript Fundamentals Every Beginner Should Master

Variables, functions, arrays, objects, and the DOM — the plain JavaScript every framework is built on top of.

Async/Await and Promises in JavaScript: A Practical Guide

How Promises actually work, why async/await is just readable syntax on top of them, and the parallel-fetching mistake almost everyone makes.

Modern JavaScript (ES6+) Features You Should Be Using

Modern JavaScript (ES6+) Features You Should Be Using

Template literals, destructuring, optional chaining, and modules — the JavaScript you actually see in every modern codebase.

Esc