Why performance work is iterative

Performance is arguably the most important metric a site has. Users stay, read and buy when pages are fast. Akamai's 2017 study found that a 100ms delay in page load can cut conversions by 7%, and every additional 100ms costs 1% of sales — at the time of the study, a single second of slowdown was worth $1.6 billion to one large retailer. Google's 2018 industry benchmarks show a similarly sharp effect of each second on bounce rates.

Source: Google/SOASTA Research, 2018.

The reverse also holds: Firefox cut average page load time by 2.2 seconds and gained 60 million extra downloads per year. Google also weighs speed when ranking mobile placements, so a slow site can be buried in search results regardless of how it performs on other metrics.

The site examined here is deliberately plain — simple HTML, CSS and JavaScript, no framework, with social sharing buttons as its most complex part. Its slowness has nothing to do with complexity, which makes it a useful testbed. The code is on GitHub; a live copy is hosted here.

Because performance is tied to everything we build, the sensible approach is incremental: pick off low-hanging fruit first, then weigh the bigger, longer-term efforts. The focus below is on those incremental wins rather than an exhaustive checklist.

Auditing with Lighthouse

Lighthouse is a Google service, available directly in Chrome DevTools, that audits performance, accessibility, SEO and best practices. It provides the before-and-after measurements for the work described here.

On the bright side, we’re one-third of the way to our goal!

Step 1: Remove redirects

The first request to the site gets redirected. The site moved from one URL to another, so anything referencing the old address bounces through a redirect first. Redirects usually add little latency, but they are cheap to check and cheap to remove.

Inspecting network requests in DevTools — or a tool like Fiddler or Postman — reveals old URLs still in play. The GitHub account was renamed from anonrobot to kealanparr, so everything stayed the same except the domain. The initial request hits https://anonrobot.github.io/redirect-to-slow-site/ and HTML-redirects to https://anonrobot.github.io/slow-site/.

Repointing references to the updated URL lets users land there directly, removing latency from the initial load. Postman is an option if DevTools isn't enough, but DevTools covers this.

Step 2: Unblock the critical render path

Profiling with the DevTools Performance panel shows how the page loads over time. The goal is to stop the browser from waiting on resources before it can render. Rendering means turning HTML, CSS and JavaScript into an interactive page: HTML becomes the DOM, external scripts are fetched while the HTML is parsed, CSS becomes the CSSOM, DOM and CSSOM combine into the render tree, layout positions everything, and paint draws it.

That process stalls when the browser must wait for resources. The two most common render-blocking resources are:

  • A <script> in the <head> without async, defer or a module attribute.
  • A <link rel="stylesheet"> without a disabled attribute and without a media attribute matching the user's device.

Fonts can block too, but those two are by far the most common. The browser treats the page as unfinished because it cannot know whether a pending download expects further work, such as styling or color changes, so it assumes the worst and blocks rendering. A stylesheet with media="print", by contrast, is only downloaded when the user prints the page and blocks nothing beforehand.

<link href="printing.css" rel="stylesheet" media="print">

Step 3: Stop blocking parser and chained requests

JavaScript can also block parsing — the step that makes HTML elements part of the DOM — whenever a script must run immediately. Scripts that don't need to block can be downloaded asynchronously: async fetches the asset asynchronously while parsing continues, and defer waits until page construction finishes before running it.

There's a trade-off between inlining JavaScript, which avoids a network request, and keeping it in an external file for modularity and reuse; the best choice depends on the use case. Once an asset has arrived, applying CSS and JavaScript performs identically whether inlined or external — inlining only removes the request time. The aim is to defer what can be deferred and keep every asset as small as possible.

The site chains multiple critical requests, forcing the browser to read a line of HTML, wait, read the next, and check for another asset. After profiling, async was added to blocking external scripts that aren't needed before rendering, such as the social media <script>.

Profiling the slow site reveals what assets are loading, how big they are, where they are located, and how much time it takes to load them.

Chrome handles at most six inflight HTTP connections per domain and queues further requests until one returns, which makes multiple critical assets even worse for parsing. Letting the browser keep parsing shows something to the user sooner.

Step 4: Shrink the payload

Total size heavily determines load time. According to web.dev, sites should aim to stay below 1,600 KB interactive within 10 seconds, and large payloads correlate strongly with long load times. A large payload is also a cost to the user, since large downloads may demand bigger data plans. At this point the site weighs 9,701 KB — more than six times the ideal.

Find and cut unused dependencies

Assets downloaded early in development often linger unused. The Network inspector, and especially the DevTools Coverage panel, shows how much downloaded code actually runs. The site was pulling in far more than it needed. Requests returning 404s are another easy cut, since they load nothing anyway.

Coverage also showed libraries that did something but carried large amounts of unused code: React, jQuery and Vue. All three were removed with no real impact. Multiple libraries are a common real-world pattern — requirements change and a second tool gets added — which is why keeping an eye on what resources are still relevant is part of being an aware front-end developer.

Compress, minify and cache

Assets we do need don't have to be served at full size, or re-served on every visit:

  • Compression shrinks a file, such as an image, without hurting visual quality. gzip is a common algorithm for this.
  • Minification strips cruft like comments and whitespace from text assets such as scripts, sending fewer bytes.
  • Caching stores an asset in the browser so later page loads get it immediately.

For text assets — HTML, CSS and JavaScript — all three apply. At a high level, gzip finds repeated content, stores those sequences once, and keeps a dictionary-like lookup to restore them when gunzipping. A test through GIDNetwork's tool showed the site's content is 59.9% compressed, suggesting room for more. The three small CSS files were consolidated into a single styles.css — three requests for so little CSS weren't justified — and selectors unused in the DOM were dropped in the process.

The text in-between the curly braces is text that has been matched multiple times and is removed from the source text by gzip to make the file smaller. There are still unique parts of the string that gzip is unable to abstract to its dictionary, but things like { a }, for example, can be removed from wherever it appears and can be added back once it is received. (View the full example)

Images are the most common asset request: HTTP Archive data from 2016 to 2021 puts median image transfer at 948.1 KB on desktop and 902 KB on mobile, more than half the ideal page budget alone. Running the site's images through Squoosh saved 40% (18.6 KB down to 11.2 KB) with no visible difference. The same can be done before upload with a desktop tool like ImageOptim, or as part of the build. Resizing, lowering quality and changing the color palette cut size further, though ideally that happens on the original asset in image editing software.

Finally, caching was being explicitly disabled in the HTML:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">

Removing that line lets browser caching take effect.

Step 5: Serve through a CDN

The traditional path is a request to your server and a wait for the response. When the user is far from the server, the bytes travel farther and the delay grows even if everything else is fast. A CDN is a set of distributed servers that delivers content from a location closer to the user.

Source: “Adding and Leveraging a CDN on Your Website”

jQuery, hypothetically still needed, is a good CDN case. One widely cited reason — that a visitor may already have the file cached from another site, given that 75.49% of the top million sites use jQuery — no longer applies: in 2020, browsers including Safari and Chrome began cache partitioning, so assets aren't shared across sites; the file still caches per site. The remaining benefit is distance: it doesn't have to travel as far. Pulling jQuery from Google's CDN is a single reference:

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

That serves jQuery considerably faster than a request to the site's own server.

Before and after

Re-profiling shows the results of the changes. Recall the starting point:

This image has an empty alt attribute; its file name is image-1024x468.png

And after:

Requesting assets optimally, deferring what doesn't need to load early, and reducing overall site size all put a functional, fully interactive page in front of the user as fast as possible.