Measuring Performance, Lighthouse 6 and Core Web Vitals by Google.
Tags: webperf, webdev, beginners
Published: 2020-07-21 15:23:47


<br/>

As web developers, we intend to deliver best user experience over the internet, in the process of doing so we come up with new ideas and designs to make our work more interactive and creative for users. But what we misjudge is that user experience is all about the look and design, but it is more than that, it also about accessibility and interaction. To keep our users engaged and expect more visits from them we can’t just rely on the presentation of our web app, it should perform too, to user's expectations. The user's expectations are persuaded by what they are experiencing over the internet, so as a responsible developer we should meet those standards.

<br/>

So what are the standards then? Because we know that performance is relative. How we make a claim that whether a web app is performing or not?

<br/>

## How to Measure Performance?
 
<br/>

When talking about the performance, we generally speak in terms of time

- How much time does it take for our page to load,
- Time taken to perform an action requested by the user

, but there are many nuances to it. 

<br/>

How much time is enough for the user to wait, or what content can be considered meaningful, when the first time user interacted with the content, what was reaction time to that interaction? To answer all these questions we measure, to measure we use tools, tools that provide us with stats that makr more sense to the developer. These stats help us in identifying the issues with our web app and the reason behind poor performance. The most common, easy to use and popular of all the performance measuring tools is the **Lighthouse** by Google.

<br/>

## Lighthouse 
 
The Lighthouse provides audits for performance, accessibility, PWA, SEO, and more. A report is generated for every lighthouse run. The performance measured by the lighthouse is distributed over many metrics, each of them has a respective weightage towards the performance score calculation. For any metric score which is not good as per Google standards, Lighthouse provides references to optimize your app. Lighthouse is also able to provide more relevant and actionable advice depending on the tools/tech used for development.

<br/>

![Screenshot 2020-07-19 at 7.51.43 PM.png](https://media.graphcms.com/kYOJaKTFSkKusVhIKU0q)

*Lighthouse report's scores screenshot*

<br/>

One can use lighthouse in the following ways
- Directly from Google Dev Tools
- Using Chrome and Firefox extensions.
- Using NPM module
- From Command Line.

Google put great analysis and research behind metrics used for calculation of performance scores. Every metric calculated is significant to page speed and page experience. Over time Google has updated the stats available for performance calculation. The Lighthouse performance testing was initially inclined towards page speed, they now have made changes to weightage in-page experience also. The latest release of Lighthouse v6 uses a different set of metrics from successor v5. The newly added stats to v6 are now more inclined towards user experience, and factors in the metrics that matter most to users. You can find how the score is calculated in the lighthouse at https://googlechrome.github.io/lighthouse/scorecalc/

<br/>

**NOTE** ✍🏽: If you had used Lighthouse then you should check your Lighthouse score again as v5 and v6 use different parameters to calculate performance score.

<br/>

## Core Web Vitals

Core web vitals are the three parameters that were introduced by Google to provide unified guidance and of which two are an addition to the latest Lighthouse v6. The Core web vitals are considered to be more user-centric and tend to measure real-world experience. These parameters will be evolving in the future as Google revaluates them over time as data is analyzed, users' behavior is monitored and development practices are updated.

<br/>

![Screenshot 2020-07-15 at 3.33.37 PM.png](https://media.graphcms.com/O6mSIjmRF6iuWzrxEaDX)

<br/>

Currently, core web vitals metrics include

<br/>

[Largest Contentful Paint (LCP)](https://web.dev/lcp/)  : measures loading performance. As websites are loaded in stages, we need to know when meaningful content is available for users to consume. The Largest Contentful Paint (LCP) metric reports the render time of the largest image or text block visible within the viewport, so showing loader is not good enough. LCP metric is updated for every element update happening in the viewport.

<br/>

[First Input Delay (FID)](https://web.dev/fid/) : measures interactivity. FID measures the time from when a user first interacts with a page to the time when the browser can begin processing event handlers in response to that interaction. This includes interaction like opening a menu, entering data to input fields or button click, or even scrolling in case of pages like a blog post.

<br/>

[Cumulative Layout Shift (CLS)](https://web.dev/cls/) : measures visual stability. CLS measures the total of all individual layout shift scores for every unexpected layout shift that occurs during the entire lifespan of the page. A layout shift occurs any time a visible element changes its position from one rendered frame to the next. 

Unlike the Lighthouse where we generate a report for a page of our site, we can measure core web vitals live for every page load happening on our browser with real-time updates, so we know now which page needs little care.

You can use the extension https://goo.gle/vitals-extension created by [Addy](https://twitter.com/addyosmani) for web vitals metrics information.

<br/>
<br/>

![Screenshot 2020-07-15 at 3.20.45 PM.png](https://media.graphcms.com/bjM9HFtSxudqwoj5doP1)

<br/>
<br/>

Tip 👀 : The extension comes with an overlay feature that can be enabled from the extension’s option.

<br/>

 We can also use the web-vitals javascript library in our project for real user monitoring.

```js
import {getCLS, getFID, getLCP} from 'web-vitals';

function sendToAnalytics(metric) {
 const body = JSON.stringify(metric);
 // Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
 (navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) ||
  fetch('/analytics', {body, method: 'POST', keepalive: true});
}

getCLS(sendToAnalytics);
getFID(sendToAnalytics);
getLCP(sendToAnalytics);
```

<br/>

## Optimize

Once we have measured the performance per page using all three core web vitals metrics, the next step is to optimize our page with the score available to us.

To optimize is to reduce.

> Less of everything, less of JavaScript, less of network resources, less of the main thread, less of render time. - Justin Ribeiro

<br/>

On road to optimization, you can use the following tips from industry experts

- **Data Fetching**: Make sure you are using nearby CDN and the data processing is fast and response time for your request is low.
- **JS**: Minimize or defer JS, remove any non-critical third-party scripts, establish third-party connections early using reconnect and prefetch.
- **Image**: Provide dimensions for images, implement lazy loading and adaptive images.
- **Caching**: Use caching for static assets, this helps content load faster on subsequent page visits from users.

<br/>

I would highly recommend going through the following guides from Google that offer specific detailed recommendations for how to optimize our pages for each of the Core Web Vitals:
- [Optimize LCP](https://web.dev/optimize-lcp/)
- [Optimize FID](https://web.dev/optimize-fid/)
- [Optimize CLS](https://web.dev/optimize-cls/)

<br/>

## Conclusion

To deliver the best user experience from our web app, we have to factor in performance along with the fancy UI. Performance is best dealt with when it is measured, make sure to measure how it is responding to all the users whether it is the user with a requirement of accessibility, or user having poor network condition or one using not so high-end mobile device, as every user counts. 

<br/>

## Resources

[Lighthouse Page](https://developers.google.com/web/tools/lighthouse/)

[Web.vitals Page](https://web.dev/vitals/)

This post was originally posted on my [blog](https://stud2design.tech/).