Frontend Web Performance Checklist
Tags: performance, javascript, webdev, tutorial
Published: 2023-04-24 06:38:01

Improving performance of web applications will always be sexy. We want the page to load faster, smoother, and without too many layout shifts (Core Web Vitals, I am looking at you 😉).  In this article I wanted to summarize all this knowledge into one single source of truth (with respect to article authors).

This summary document is based on the following articles:

* [My web performance journey with Nuxt, Storyblok & Netlify](https://www.dawntraoz.com/blog/my-web-performance-journey-with-nuxt-storyblok-netlify/) by @dawntraoz
* [How We Achieve 90+ Lighthouse Performance Score and Fully Offline Mode for DANA Home Shopping](https://medium.com/dana-engineering/how-we-achieve-90-lighthouse-performance-score-and-fully-offline-mode-for-dana-home-shopping-580b1b540c4d) by @jefrydco
* [Web Vitals, Google Search, the State Vue & Nuxt performance optimization in July 2020](https://medium.com/weekly-webtips/web-vitals-google-search-the-state-vue-nuxt-performance-optimization-in-july-2020-71441eefc51) by Johannes Lauter

and my own knowledge that I gathered throughout the years.

Make sure to visit these articles and give a solid like to all of them and their authors 😊

## Preload key requests / Preconnect to required origins

Declare preload links in your HTML to instruct the browser to download key resources as soon as possible.

```html
<head>
  <link rel="preload" href="critical.css" as="style">
  <link rel="preload" href="critical.js" as="script">
</head>
```

Consider adding preconnect or dns-prefetch resource hints to establish early connections to important third-party origins.

```html
<link rel="preconnect" href="https://example.com">
<link rel="dns-prefetch" href="https://example.com">.
```

dns-prefetch works exactly the same as preconnect but has wider browser support.

## Reduce third-party usage

Third-party code can significantly impact load performance. You can however modify the way you are using this third party library by:

* Loading the script using the async or defer attribute to avoid blocking document parsing.
* Self-hosting the script if the third-party server is slow.
* Removing the script if it doesn't add clear value to your site.
* Use link rel=preconnect or link rel=dns-prefetch to perform a DNS lookup for domains hosting third-party scripts.

## Eliminate render blocking resources

Resources are blocking the first paint of your page. Consider delivering critical JS/CSS inline and deferring all non-critical JS/styles. You can reduce the size of your pages by only shipping the code and styles that you need.

Once you've identified critical code, move that code from the render-blocking URL to an inline script tag in your HTML page.

Inline critical styles required for the first paint inside a style block at the head of the HTML page and load the rest of the styles asynchronously using the preload link.

You can read more about this [here](https://sia.codes/posts/render-blocking-resources/#how-do-i-test-my-website-for-render-blocking-resources%3F)

## Minify/Remove unnecessary CSS and JS

When you are building a big application, you will get to a place where your project may have much more code that it actually needs and uses.

Use tools like [CSS Minification](https://web.dev/minify-css/#css-minification-with-webpack) or [Terser JS Plugin](https://github.com/terser/terser).

To eliminate unused css use a tool like [PurgeCSS](https://purgecss.com/).

To eliminate unnecessary JavaScript you can use Terser mentioned previously or utilize [Tree Shaking](https://webpack.js.org/guides/tree-shaking/) to allow Dead Code Elimination. You can also use [Code Splitting](https://developer.mozilla.org/en-US/docs/Glossary/Code_splitting) which will split code into bundles that can be loaded on demand.

### Scan modules for duplicates

Remove large, duplicate JavaScript modules from bundles to reduce final bundle size.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8jgv7d98jpb0cwdz2gt.png)

Use [Webpack Bundle Analyzer](https://www.npmjs.com/package/webpack-bundle-analyzer)

## Reduce execution time

The combination of code splitting, minification and compression, removal of unused code and caching techniques will greatly improve execution time.

Consider reducing the time spent parsing, compiling and executing JS. You may find delivering smaller JS payloads helps with this.
The idea is to optimize both our JS and CSS code, minimizing it and removing unused code, as well as the third-party libraries we are using.

Keep the server response time for the main document short because all other requests depend on it.

You can read more about this [here](https://gtmetrix.com/reduce-javascript-execution-time.html)

## Image handling

### Properly size images

Serve images that are appropriately-sized to save cellular data and improve load time.

```html
<img src="cat-large.jpg" srcset="cat-small.jpg 480w, cat-large.jpg 1080w" sizes="50vw">
```

You can read more about this [here](https://web.dev/uses-responsive-images/?utm_source=lighthouse&utm_medium=cli)

### Efficiently encode images

Optimized images load faster and consume less cellular data.
Using your image CDN service or the compression of your image should be enough.

You can read more about this [here](https://web.dev/uses-optimized-images/)

And also you can read an article that I have released some time ago [here](https://dev.to/jacobandrewsky/optimizing-images-in-js-apps-with-ipx-37b1)

### Serve images in next-gen formats

Image formats like WebP or Avif often provide better compression than PNG or JPEG, which means faster downloads and less data consumption.

You can read more about this [here](https://web.dev/uses-webp-images/)

### Image elements have explicit width and height

Set an explicit width and height on image elements to reduce layout shifts and improve CLS.

You can read more about this [here](https://gtmetrix.com/use-explicit-width-and-height-on-image-elements.html)

### Preload largest contentful paint (LCP)

Preload the image used by the LCP element in order to improve your LCP time.

```html
<link rel="preload" href="/path/to/image.jpg" as="image">
```

```js
head() {
 return {
    link: [
      {
        rel: 'preload',
        as: 'image',
        href: 'path/to/lcp/image',
      },
    ],
  }
}
```

You can read more about this [here](https://gtmetrix.com/preload-largest-contentful-paint-image.html#:~:text=Preloading%20the%20Largest%20Contentful%20Paint,sooner%2C%20enhancing%20their%20user%20experience.)

## Fonts

### All text remains visible during webfont loads

Leverage the font-display CSS feature to ensure text is user-visible while webfonts are loading.

```css
@font-face {
  font-family: 'Arial';
  font-display: swap;
}
```

The font-display API specifies how a font is displayed. swap tells the browser that text using the font should be displayed immediately using a system font. Once the custom font is ready, it replaces the system font.

For Google fonts, for example, is as simple as adding the &display=swap parameter to the end to the Google Fonts URL:

```html
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700&**display=swap**" rel="stylesheet">
```

You can read more about this [here](https://web.dev/font-display/)

## What to avoid?

### Large layout shifts

Cumulative Layout Shift (CLS) is a Core Web Vitals metric calculated by summing all layout shifts that aren’t caused by user interaction.

### Avoid an excessive DOM size

A large DOM will increase memory usage, cause longer style calculations, and produce costly layout reflows.

### Multiple page redirects

Redirects introduce additional delays before the page can be loaded.

### Serving legacy JavaScript to modern browsers

Polyfills and transforms enable legacy browsers to use new JavaScript features. However, many aren't necessary for modern browsers.


### Enormous network payloads

Large network payloads cost users real money and are highly correlated with long load times.

* Defer requests until they're needed. 
* Optimize requests to be as small as possible, minimizing and compressing, try to use WebP for the images when it's possible. An image CDN will be always there to keep our performance up!
* Cache requests so the page doesn't re-download the resources on repeat visits.

### Document.write()

For users on slow connections, external scripts dynamically injected via document.write() can delay page load by tens of seconds.

### Non-compositioned animations

Animations which are not composited can be heavy and increase CLS. Use `translate` and `scale` CSS properties instead.

## Summary

You now know more about improving web performance. Just please remember that improving performance is not an issue that you can just sit once and fix. It is a continuous process and the topic of performance should be addressed regularly so that new features of your website (for sure needed) won't break the performance.