Discover more from Unstacked Labs
Web Performance - Key metrics to ensure your website is Performant.
In this issue, we are going to go through on why you should care about the performance of your website and important metrics you can use to measure performance and improver user experience.
Over the next few weeks, I will be speaking at Google DevFest events regarding measuring your website performance. After previous talks that I gave, I have come to the realization that having an article that goes into detail in relation to the topic is important, as it gives attendees a place where they can learn more about the topic that’s better than slides.
On top of that, I also thought this would be a great article for my first-ever post here at Unstacked, a new newsletter that appeals to a broader developer audience, in contrast to my other newsletter - All Things Typescript (whispers - subscribe 😉).
Without wasting any more of your time, let’s dive into today’s issue about web performance and key metrics that you should always keep an eye on.
Web performance is all about making websites fast, including making slow processes seem fast.
Why care about your website performance
Key Metrics to use to measure your Website Performance
Websites are a key component of our day-to-day lives. They connect us to vital information, and key services such as Government services, connect with people, and do business among so many things. This means it’s essential that our website provide a decent user experience.
The problem that exists is that while our website may behave as expected on our devices, our users won’t be accessing the device from our devices, but their own and under different circumstances.
This reminds me of this tweet I came across last year, which sadly I didn’t save, which had the following quote:
Your iPhone isn’t the real world
It’s important as web developers to remember that outside our comfort zone (developers are mostly well paid with access to the latest technologies at their hands), our users are not going to be in ideal locations to access our websites hence the above quote.
For instance in Africa, most people have budget smartphones, with low memory and CPU processing capabilities, and also a gotcha, and these people also live in areas with poor network connectivity such as rural areas.
As developers, we need to ensure that our websites have a decent if not great user experience for users with the cheapest devices and poor network conditions - the lowest common denominator as well for people with more expensive devices and great internet connection.
So, how do you know a website is performing well or poorly? How do you measure the performance of a website? This is where Web Vitals comes in, an initiative by Google to provide unified guidance for quality signals that are essential to delivering a great user experience on the web.
Web Vitals are metrics such as how long it takes for the user to see content on the web page they visited that we can use to understand how well your website is doing. If your website has good performance in these metrics, then the user experience is most likely great, otherwise poor.
Core Web Vitals
We are not going to go into all Web Vitals in this issue, I will leave that up for a future issue. Core Web Vitals are a subset of Web Vitals (goes without saying) and apply to all websites. Each vital in the Core Web Vitals measures important and distinct user experience metrics and reflects the real-world performance of your website, and the list will evolve over time, changing as needed.
As of the time of writing this issue, here are the core web vitals:
Largest Contentful Paint (LCP)
The Largest Contentful Paint (LCP) metric measures the render time of the largest block (text or image) that is visible within the viewport, from the point the page starts to load. This considers images, videos, text blocks, the first frame of animated images, and auto-playing videos, etc.
For your site to have a good LCP score, you need to achieve a score of 2.5s or less, ideally in 75% of all page loads on your websites.
Optimize for the Largest Contentful Paint
When you measure LCP either using Lighthouse or PageSpeed Insight, it usually provides a list of reasons why your LCP was given a certain score and how you can optimize for it.
If you have a website with a poor LCP score, here are some of the things you can do to optimize for LCP.
Improve your server response time
Optimize for Resource Discovery on your Web Page
Resources impacting LCP should always be loaded as early as possible. Your browser ships with a preload scanner, which will go through your raw HTML before it’s parsed by the browser and look for items it can prefetch such as images, and rel=preload Links.
To optimize for resource discovery:
ensure that all images have the image URL set inside
CSS Background Images and Fonts are preloaded using the
<Link rel=preload />.
Give Priority to Important Resources to Loaded Upfront
After optimizing for discovery, it is important to ensure discovered resources are loaded before non-important ones. Resources such as images, that are non-blocking aren’t usually given priority by the browser even when they are discovered by the preload scanner. We can use the fetch-priority attribute to hint to the browser that an image is important and should be given priority.
<img fetchpriority="high" src="/path/to/important/image.webp">
We can also de-prioritize resources, to ensure that we are not taking too much of the browser bandwidth and have enough of it to load important resources.
Use modern image formats such as WebP. You can use a specialized image CDN that will automatically serve an image format compatible with the user's device.
Use Responsive Images. Don’t use one large image for all screen sizes, but instead use responsive images.
Compress Images to the smallest possible size, without impacting the quality too much.
Use appropriate cache settings ensuring that network latency is reduced and only necessary for the initial load of the resource
Reduce font size
Cumulative Layout Shift (CLS)
One of the most frustrating things to users is when the content they are reading suddenly moves to a different location. This could be caused by things such as an image loading above the content, pushing the content down or some other content being added dynamically such as ads and the content has to move to create space for it.
This is quite difficult to see as a developer as we are usually lucky enough to be using fast or decent-speed internet, images are already cached, which reduces the load time for when we are testing.
CLS is an important metric as it measures unexpected layout shifts during the lifespan of a webpage. A layout shift occurs anytime visible elements to the user suddenly change positions and CLS helps us understand how often our web page is doing this.
There are two types of layout shifts to keep in mind:
unexpected layout shift - it wasn’t intended by the developer as part of the web page design and is bad,
and the expected layout shift - think of user-initiated layout shift, like when they click a button to expand a section or animated content.
Ideally, you should have zero layout shifts on your web page, but a good score is 0.1 or less for 75% of all page loads on your website.
To optimize for CLS, ensure that all your images have a fixed height and width, or add aspect ratio to them, so that the browser can reserve a place for them in the rendered page.
First Input Delay (FID)
Ever visited a web page and after it loaded, and you tried to click on a link or button and it took a while for the website to respond? First Input Delay (FID) measures this specific metric of how long it takes for the web page to respond to user interaction such as clicks, taps, and keyboard events during the loading process. For a good FID score, it should have a delay of 100 milliseconds or less.
In order to optimize for FID, you can do the following:
Explore on-demand loading of Scripts as if you load too much on the main thread, it will slow down user interactions.
Code-splitting or lazy loading and loading the smaller chunks on demand.
And finally, minimize the use of pollyfills.
Interaction to Next Paint (INP)
Ever found yourself on a very sluggish website, where the page was slow to respond and even freezes once in a while? Interaction to Next Paint or INP is a metric that’s designed to measure the general interactivity of your website. Unlike the First Input Delay (FID) metric that focuses on time to first interaction, this focuses on all interactions on your website.
INP measures the latency of keyboard interactions, taps, and clicks during the lifespan of the site. The value of the longest interaction is the INP value, with any outliers ignored. A good INP is below 200 milliseconds.
You can do the following to optimize your INP score:
Avoid having heavy and long-running tasks on the main thread as they can block other processes hence impacting the performance of your site.
Minimize the size of the DOM - with large DOM elements, the rendering work takes longer and hence impacts the performance of your Website. With large DOM elements, both the initial rendering and any rerendering afterward are going to be very expensive and slow.
Server Side Rendering (SSR) where this work is done on the server instead so we can send HTML to the client,
and Lazy Loading, where we break up our application into smaller chunks that can be loaded as needed. As developers, however, we need to be aware of this and avoid it where necessary.
In this issue, we talked about Web Performance and important metrics to keep an eye on in order to ensure the user experience is decent if not great. We learned that it’s important to ensure that the lowest common denominator users get a good user experience and can use your website without struggling too much. It’s important for web developers to ensure a website with great performance and great user experience.