Skip to main content
Saved

Performance as a Foundation

foundation
14 minute read
written by
den odell
added

Performance belongs in the architecture from day one, instead of being bolted on after users start complaining.

lcp targetThe main content of the page should be visible in under 2.5 seconds.

TL;DR

Performance is an architectural constraint. It affects your decisions about everything from data fetching to deployment, so you can’t leave it until the end.

  • The decisions you make on day one limit how fast your app can be later
  • Measurements from real users tell you more than synthetic benchmarks do
  • Users judge your app by how fast it feels, so perceived speed counts too
  • A performance budget only works when your CI pipeline enforces it

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

Donald Knuth

The Retrofit Trap

I’ve watched teams build applications for months, shipping features quickly and celebrating how fast they’re moving, until someone notices the app feels slow and users start complaining. Someone opens the browser’s developer tools and finds a 3MB JavaScript bundle and a waterfall of blocking requests.

“We’ll optimize it later,” someone said six months ago. Now “later” has arrived, and the only way to make the app faster is to rewrite its core architecture.

Performance is hard to retrofit because the slow parts are built into your architecture. The decisions you made about data fetching and component boundaries now limit what you can change.

I’ve seen the same thing happen at startups and at large companies, in teams that treat performance as an optimization rather than a constraint. They ship quickly, and then they find out the app they’ve built is slow. If your team works that way, you might not find out until your users tell you.

The way to avoid that is to treat performance as part of your architecture from the start, instead of hoping you’ll find a way to optimize the app later.

Defining Performance

There isn’t one number that tells you how fast your app is. There are several metrics, and together they describe how your users experience it.

The Metrics to Watch

Loading
FCP First Contentful Paint
The moment the first content appears. The user is no longer looking at a blank screen.
Loading
LCP Largest Contentful Paint
The moment the main content is visible. Users can start reading or using what they came for.
Under 2.5s
Loading
TBT Total Blocking Time
The total time long tasks keep the main thread busy while the page loads. Taps and clicks during that time have to wait.
Runtime
INP Interaction to Next Paint
How long the page takes to show a response after a click, tap, or key press, across the whole visit. A low value means none of those interactions feels sluggish.
Under 200ms
Runtime
CLS Cumulative Layout Shift
How much the content moves around unexpectedly. A low value means content doesn't move as the user goes to click, so they don't click the wrong thing.
Under 0.1

Highlighted: the three Core Web Vitals, with the targets from the budgets later in this article.

LCP, INP, and CLS are the three Core Web Vitals, which are Google’s metrics for measuring user experience. They’re useful because they track closely with how users feel about your app.

A user doesn't know their LCP was 2.4 seconds. They know the page felt fast or felt slow. The metrics are a way of measuring that experience, and it's the experience we're trying to improve.

You can improve the metrics while making the experience worse, and you can make the experience better without changing the metrics. A skeleton screen doesn’t speed up data loading, but it makes the loading time feel predictable instead of frustrating.

Performance as Architecture

Architectural Decisions That Determine Performance

You can't bolt performance on later, because the decisions you make early on about the architecture limit how fast the app can be.

Your data fetching strategy determines whether pages load quickly or slowly. Fetching data on the client after JavaScript executes is fundamentally slower than fetching on the server before HTML is sent. Optimizing the client code doesn’t change that, because the request still can’t start until the JavaScript has run.

Your component structure affects how big your bundles are and where you can split them. If components import everything, there’s nowhere useful to split the bundle, and dependencies that cross feature boundaries pull in code that could otherwise be lazy loaded.

Your rendering approach sets when users first see content. With client-side rendering, users wait for JavaScript to download, parse, and execute before anything appears. With server-side rendering, the content arrives with the HTML. You make that choice when you design the architecture, and you can’t change it later with a config flag.

Your state management controls how much of the page re-renders when data changes. If a change to global state re-renders unrelated components, small optimizations won’t fix it, and you’ll need to change how the state is structured.

If someone asks “can we make this faster?”, the honest reply is often “not without changing how it’s built.” If you wait until the end to think about performance, the architecture is already set, and all that’s left are small optimizations that can’t undo the big decisions.

The Performance Budget

A performance budget is a set of hard limits for your performance metrics. Your team enforces those limits the same way it enforces any other project requirement.

Setting Budgets

Metrics to Budget

  • Bundle size: Total JavaScript, per-route JavaScript, and CSS
  • Request count: Number of requests for the initial load
  • Core Web Vitals: LCP under 2.5s, INP under 200ms, CLS under 0.1
  • Image weight: Total bytes of images per page
  • Third-party scripts: Bytes and blocking time from external code

Ways to Enforce

  • CI checks: The build fails when the bundle goes over budget
  • Bundle analyzers: Show you what’s in your bundles
  • Lighthouse CI: Runs performance tests automatically
  • Real user monitoring: Tracks metrics in production
  • Alerts: Let the team know when a metric gets worse

If nothing enforces the budget, the team will probably ignore it when a deadline is close.

If a new feature would push your bundle over budget, you have to choose between optimizing existing code, removing something else, or going over the budget knowingly. Without a budget, the feature would ship and the bundle would grow, and the team probably wouldn’t notice. Every byte adds a little to how long users wait for the page, and with a budget in place, the team has to decide whether each addition is worth the extra time.

To set your initial budgets, you need to know what devices and networks your users have. A budget that suits users on fiber with flagship phones is far too generous for users on 3G with low-end Android phones.

Perceived Performance

Users notice whether the app feels fast or slow more than they notice the actual milliseconds. To improve perceived performance, you change how the slow moments feel, even if they take the same amount of time.

Techniques for Perceived Speed

Show Progress

Skeleton screens show the layout of the content before it arrives, so users see something instead of a blank screen and can tell the page is working.

Progress indicators show that something is happening. A determinate progress bar is better than a spinner, and a spinner is better than nothing, because when nothing changes on screen, users tend to assume something is broken.

Optimistic updates show the result before the server confirms it. If a user likes a post, it shows as liked right away, and the like is undone if the server rejects it, so the app feels responsive even when the network is slow.

Reduce Perceived Waiting

Preloading fetches resources before they’re needed. If you preload the next page when the user hovers over a link, it can appear almost right away when they click, so navigation feels fast.

Progressive loading shows content as it arrives. Text arrives before images, and low-resolution images before high-resolution ones, so the user has something to look at sooner.

Instant feedback shows that each action has registered as soon as it happens. A button that changes as soon as it’s pressed, instead of when the work finishes, doesn’t leave the pause that users notice as lag.

You want your app to feel fast as well as be fast. Sometimes that takes real speed improvements, and sometimes it takes better feedback about what’s happening.

Animation helps here too. I’ve noticed that a 300ms animation that plays while content loads feels like a designed transition, while the same 300ms with an interface that doesn’t respond feels like lag.

The Critical Rendering Path

Every web page has a critical rendering path: the steps the browser goes through between receiving the HTML and showing usable content. If you know what’s on that path, you know what’s delaying the first render.

The Path to First Render

  1. HTML parsing begins: The browser reads HTML and builds the DOM
  2. CSS blocks rendering: Stylesheets must download and parse before layout
  3. JavaScript can block everything: Scripts in <head> without async or defer stop parsing
  4. Layout calculates positions: Once DOM and CSSOM are ready, layout begins
  5. Paint shows pixels: Finally, content appears on screen

Each resource on this path delays rendering, so you want the path to be as short as possible, with few blocking resources, critical CSS inlined, scripts deferred, and everything else loaded after first paint.

Stylesheets in <head> block rendering, which is why critical CSS is often inlined, and scripts without defer block parsing, so they get defer or move to the end of the body. Third-party scripts add unpredictable blocking time, so they’re best loaded asynchronously, or not at all.

JavaScript: The Biggest Lever

In most frontend apps, JavaScript has the biggest effect on performance, and many performance problems start there.

The Cost of JavaScript

JavaScript takes more work for the browser than other assets. Images only have to be downloaded, decoded, and displayed, but the browser has to download, parse, compile, and run JavaScript. Each step takes time, especially on mobile devices with slower CPUs.

A 500KB image is a 500KB download. A 500KB JavaScript bundle is a 500KB download plus the CPU time to parse, compile, and execute it. On a budget Android phone, that processing can take longer than the download itself.

This is why bundle size is a bigger problem for JavaScript than for other assets. Every kilobyte of JavaScript adds processing time that doesn’t show up in transfer size metrics.

Managing JavaScript

You can ship less JavaScript by questioning each dependency you add. An animation library might be convenient, but CSS may be able to do the same job. A utility library saves you time, but tree-shaking might not remove the parts you don’t use.

If you split your bundles, users visiting the homepage don’t have to download the settings page code. With route-based code splitting, users only download JavaScript for the pages they’re using, and dynamic imports let you split at any boundary.

It also helps to defer what you can, because not all JavaScript needs to run immediately. Analytics and third-party widgets can both load after the critical JavaScript.

Before you think about how to optimize your JavaScript, it’s worth asking how much of it you need. If you start with none and only add what’s essential, you’ll end up shipping much less of it.

Server-Side Rendering and Beyond

Your code’s performance depends a lot on where and when it runs.

Rendering Strategies

Client-Side Rendering (CSR)

The browser downloads the HTML shell, then the JavaScript, and then the JavaScript fetches data and renders the content.

Trade-offs:

  • Slowest first paint
  • Blank screen until JavaScript runs
  • Poor SEO without additional work
  • Simplest deployment, as static files

Server-Side Rendering (SSR)

The server renders HTML with the content, the browser displays it as soon as it arrives, and JavaScript hydrates it for interactivity.

Trade-offs:

  • Fastest first paint
  • Content visible before JavaScript
  • SEO-friendly out of the box
  • Requires server infrastructure

Static Site Generation (SSG) pre-renders pages at build time, combining SSR’s fast first paint with CSR’s simple deployment. It’s a good fit for content that doesn’t change between requests.

Incremental Static Regeneration (ISR) lets static pages be regenerated after deployment, on a schedule or on demand, so you can keep content fresh without giving up the speed of static files.

Streaming SSR sends HTML progressively as it’s generated, which improves Time to First Byte and lets content appear before the full page is ready.

Choosing between these is an architectural decision as well as a deployment one, because it limits how fast your pages can be, so it’s worth deciding early and on purpose.

Modern meta-frameworks like Next.js, Nuxt, SvelteKit, and React Router, which Remix merged into, support multiple strategies, sometimes on the same page, so you can choose a strategy for each page instead of one for the whole app.

Images and Media

After JavaScript, images are usually the biggest cause of slow loading. Unlike JavaScript, they don’t block rendering, but they use bandwidth and can cause layout shifts.

Image Optimization

Format and Size

  • Modern formats: WebP and AVIF offer 25-50% smaller files than JPEG
  • Responsive images: Different sizes for different screens
  • Lazy loading: Images only load when they’re near the viewport
  • Compression: The right balance of quality and size for what each image is used for

Layout Stability

  • Explicit dimensions: A width and height set on each image
  • Aspect ratio boxes: Space reserved before images load
  • Placeholders: Blur-up or color placeholders while images load
  • Priority hints: Telling the browser which images to load first

The <picture> element and srcset attribute enable responsive images, and loading="lazy" defers offscreen images. The fetchpriority attribute tells the browser how urgent each image is. Because these are built into HTML, the browser can act on them while it parses the page, before any JavaScript runs.

Measuring Performance

Before you can improve performance, you need to measure it, and the two main kinds of measurement tell you different things.

Lab Data vs. Field Data

Lab Data Synthetic Testing Controlled tests on known devices and networks, using tools like Lighthouse and WebPageTest. Field Data Real User Monitoring Metrics from real users on real devices and networks, including Core Web Vitals and custom metrics.
Strengths
  • Reproducible results
  • Useful for debugging specific issues
  • Runs before deployment
  • Detailed diagnostics
  • Reflects real experience
  • Captures device diversity
  • Shows geographic variation
  • Reveals edge cases
Weaknesses
  • Doesn't reflect real users
  • Misses real-world conditions
  • Can be gamed
  • Harder to debug
  • Can't test unreleased code
  • Needs a lot of traffic to be significant

You need both, for different jobs: lab data helps you diagnose and debug, and field data tells you whether users see the improvements. A Lighthouse score of 100 doesn’t help if real users on real devices still find the app slow, so the field metrics are the ones to improve.

Performance Culture

Performance is something the whole team keeps working at. You might make your app fast once, and then find it’s slow again a few months later, because features kept piling up and the team stopped checking how each one affected speed.

Building Performance Into Process

It helps to make performance visible, with real user metrics on a dashboard the team can see, and someone who investigates each regression.

Your team can treat regressions as bugs. If a deploy increases LCP by 500ms, that should block the release the same way a functional bug would.

Performance can be part of the code review too, with reviewers asking whether a new dependency is necessary, or whether a pattern will cause extra re-renders.

Testing on real devices shows the team what many users experience, and keeping a few low-end Android phones around for testing makes that easy. A developer’s MacBook is much faster than a typical user’s phone, and it’s easy to forget that until you use the slower one yourself.

When performance is part of the culture, your team asks “how will this affect performance?” as naturally as it asks “does this work?”

The Path Forward

If you treat performance as a foundation, you treat speed as a constraint from the beginning, because it affects your whole architecture. In practice, that comes down to five things:

  1. Performance budgets, set early and enforced in CI
  2. Rendering strategies chosen with performance in mind
  3. Minimal JavaScript, with what you do ship split into smaller bundles
  4. Measurements from real users on real devices
  5. Deliberate trade-offs when performance conflicts with a feature

Building this way takes less work than retrofitting performance later, and your users get an app that responds quickly when they’re trying to get something done. If an app is too slow to use, it doesn’t really work, so performance can’t be optional.

Summary

  1. Performance is architectural, and the decisions you make on day one limit what’s possible later
  2. Performance budgets are requirements your team enforces
  3. Perceived performance counts as much as measured performance
  4. JavaScript usually has the biggest effect, so it helps to ship less, split more, and defer what you can
  5. Rendering strategies such as CSR, SSR, and SSG are best chosen based on performance needs
  6. You need measurements from real users on real devices as well as synthetic tests
  7. Performance becomes part of your team’s culture through visibility, code reviews, testing on real devices, and treating regressions as bugs

Related Content:

External Resources:

Next Foundation · 13 min read

Resilience by Design

Systems keep working when things fail if you design for failure and recovery from the start, instead of adding error handling at the end.

Read the next Foundation →

Keep Exploring

A monthly email
from Den Odell.

What I'm thinking about frontend patterns, plus news when the site changes.

No spam. Unsubscribe anytime.