Core Web Vitals: Measure and Improve Web Performance
Why Core Web Vitals Matter for Your Website
You've probably heard about Core Web Vitals (CWV) — Google's metrics for user experience. But if your site feels slow or janky, you're not alone. Many developers struggle to translate CWV scores into concrete fixes. This guide walks you through measuring and improving each vital with practical steps and code snippets.
Core Web Vitals are three specific metrics: Largest Contentful Paint (LCP) for loading performance, First Input Delay (FID) for interactivity, and Cumulative Layout Shift (CLS) for visual stability. Google uses these as ranking signals, so optimizing them benefits both users and SEO.
Understanding the Three Core Web Vitals
Before diving into fixes, let's clarify what each metric measures and the thresholds for a good score.
| Metric | Measures | Good | Needs Improvement | Poor |
|---|---|---|---|---|
| LCP | Loading performance | ≤ 2.5s | 2.5s – 4.0s | > 4.0s |
| FID | Interactivity | ≤ 100ms | 100ms – 300ms | > 300ms |
| CLS | Visual stability | ≤ 0.1 | 0.1 – 0.25 | > 0.25 |
Note: FID is being replaced by Interaction to Next Paint (INP) in March 2024, but the optimization principles are similar. We'll focus on FID here as it's still widely referenced.
How to Measure Core Web Vitals
You need both lab and field data to get a complete picture. Lab data (synthetic tests) helps debug, while field data (real user monitoring) shows actual user experience.
Lab Tools
- Lighthouse in Chrome DevTools: Run an audit in the Lighthouse tab.
- PageSpeed Insights: Provides both lab and field data for a URL.
- WebPageTest: Advanced testing with custom network conditions.
Field Tools
- Chrome User Experience Report (CrUX): Public dataset of real-user metrics.
- Google Search Console: Core Web Vitals report shows group performance.
- web-vitals JavaScript library: Collect metrics from real users in your analytics.
To collect field data, include the web-vitals library and send metrics to your analytics endpoint:
<script type="module">
import {onLCP, onFID, onCLS} from 'https://unpkg.com/web-vitals@3?module';
function sendToAnalytics(metric) {
const body = JSON.stringify(metric);
navigator.sendBeacon('/analytics', body);
}
onLCP(sendToAnalytics);
onFID(sendToAnalytics);
onCLS(sendToAnalytics);
</script>
This gives you real-world data to complement lab tests.
Improving Largest Contentful Paint (LCP)
LCP measures when the largest content element (image, text block, or video) becomes visible. To improve it, focus on server response time, resource loading, and render-blocking resources.
1. Optimize Server Response Time
- Use a CDN to serve content closer to users.
- Enable caching (HTTP cache headers, server-side cache).
- Upgrade hosting if TTFB (Time to First Byte) is consistently high.
2. Optimize Images
- Compress images and use modern formats like WebP or AVIF.
- Use responsive images with
srcsetandsizes. - Lazy-load below-the-fold images, but avoid lazy-loading the LCP image.
For quick image compression, try our Image Compressor.
3. Eliminate Render-Blocking Resources
- Inline critical CSS and defer non-critical CSS.
- Use
asyncordeferfor JavaScript. - Minify CSS and JavaScript.
4. Preload Important Resources
Use <link rel="preload"> for key assets like fonts or hero images:
<link rel="preload" href="/hero.webp" as="image">
Improving First Input Delay (FID)
FID measures the time from when a user first interacts with your page to when the browser can respond. Long JavaScript tasks block the main thread, causing delays.
1. Break Up Long Tasks
Split long-running JavaScript into smaller chunks using setTimeout or requestIdleCallback. For example:
function processItems(items) {
if (items.length === 0) return;
const item = items.pop();
// Process item
setTimeout(() => processItems(items), 0);
}
2. Optimize Event Handlers
- Debounce or throttle input handlers.
- Avoid complex calculations in event listeners; offload to Web Workers.
3. Reduce JavaScript Execution Time
- Code-split with dynamic imports.
- Remove unused JavaScript (tree shaking).
- Use
loading="lazy"for non-critical iframes.
4. Use Web Workers for Heavy Tasks
Move CPU-intensive work off the main thread:
const worker = new Worker('worker.js');
worker.postMessage(data);
worker.onmessage = (e) => { /* handle result */ };
Improving Cumulative Layout Shift (CLS)
CLS measures unexpected layout shifts. Common causes include images without dimensions, ads, embeds, and web fonts.
1. Always Set Width and Height on Media
Include width and height attributes on images and videos, or use CSS aspect-ratio:
img {
aspect-ratio: 16 / 9;
width: 100%;
height: auto;
}
2. Reserve Space for Dynamic Content
For ads or embeds, reserve space with a placeholder element or set a fixed size.
3. Avoid Inserting Content Above Existing Content
If you must insert a banner, use a transform animation or overlay that doesn't shift the layout.
4. Use Font-Display: swap
Prevent invisible text and layout shifts from web fonts:
@font-face {
font-family: 'MyFont';
src: url('myfont.woff2') format('woff2');
font-display: swap;
}
Monitoring and Continuous Improvement
Optimizing Core Web Vitals is not a one-time task. Set up continuous monitoring with tools like:
- Google Search Console: Alerts for CWV regressions.
- Lighthouse CI: Run audits on every pull request.
- Real User Monitoring (RUM): Tools like New Relic, Datadog, or custom analytics.
Regularly review field data and iterate on fixes. Prioritize the metric with the worst score first.
FAQ
What are Core Web Vitals?
Core Web Vitals are a set of metrics that Google uses to measure user experience: LCP (loading), FID (interactivity), and CLS (visual stability).
How often should I check Core Web Vitals?
Monitor field data continuously via Search Console or RUM. Run lab tests after major changes and at least monthly.
Does improving Core Web Vitals affect SEO?
Yes, Core Web Vitals are a ranking signal. Better scores can improve search visibility and user satisfaction.
Ready to optimize your images for better LCP? Use our free Image Compressor to reduce file sizes without losing quality.