Blog

Published · November 28, 2024

GOTTA GO FAST!

Back in 2005, one of my computer science teachers told us that most students abandon a page if it takes more than 12 seconds to load...

JavascriptWebdevPerformanceBeginners

windows xp

Back in 2005, one of my computer science teachers told us that most students abandon a page if it takes more than 12 seconds to load. That stuck with me for a long time, but I started paying closer attention in the last two years, since I gave the Web Vitals talk at WebConf.

scrolling

These days, people have an extremely short attention span—we need fast, constant access to content. Not only because of how much content exists, but also because of how technology has evolved. Because of this, web developers need to obsess over initial page load performance. Spoiler alert: when your site is slow, your users leave.

Let’s do a quick recap before we continue, in case you didn’t make it to WebConf: Google uses Core Web Vitals metrics to determine your site’s ranking.

What are Core Web Vitals?

They are metrics that measure user experience in terms of speed, interactivity, and visual stability. For example:

web core vitals

Why performance matters

Now, why should you care about frontend performance? Here are a few quick reasons:

  1. Users hate waiting. If your page takes more than 3 seconds to load, more than 50% of people leave.

  2. Google hates slowness too. Web Vitals metrics are key for SEO.

  3. It affects your business. A delay in app load time can reduce sales.

Convinced? Good. Let’s keep going.

Resource optimization

The first step is reducing resource load time.

Images

Did you know images are usually the heaviest resource on a page? Use modern formats like WebP to save space without losing quality, and don’t load every image at once—apply lazy loading so they download only when the user needs them.

Image optimization

Fonts

Love custom fonts? Me too. But they can be a problem. Limit how many fonts you use, preload the essential ones, and consider system fonts when possible.

fonts optimization

Videos

If you have videos on your site, make sure they’re optimized. Use placeholders instead of loading them immediately, and when you can, embed from external services like YouTube.

video optimization

Efficient loading techniques

This is where things get interesting. There are tons of ways to improve how and when your page resources load.

Suppose we have the following example we want to optimize:

ejemplo 1

ejemplo Code Splitting

ejemplo Lazy Loading

ejemplo Prefetching

Reducing JavaScript

one does not simply just get rid of js

JavaScript is great, but too much can be a problem. Tree Shaking removes unused parts from the final bundle—functions or modules you never call. Use tools like Webpack or Rollup to analyze your code and automatically detect what you actually need, making apps lighter and faster. Your framework might already be doing this if you use Next or Angular Universal.

Suppose we have this products.js file in the Utils folder

code example

But in cart.js we only call two functions from products.js

code example 1

What gets built is only the code we use, discarding the functions we’re not calling from cart.js

code example 3

Cache

cache image

Using the browser cache is like giving users a shortcut. Configure your static resources so they don’t download every time someone visits your page. You can use browser local storage (localStorage) or in-memory storage to save data like products, cart details, or previously loaded pages.

code example 4

code example 5

SSR, SSG, and CSR

cooking nightmare

As we look for different ways to optimize performance, we should also consider three strategies: SSR (Server-Side Rendering), SSG (Static Site Generation), and CSR (Client-Side Rendering).

When to use each one?

SSR:

SSG:

CSR:

Optimizing API calls

We don’t only improve performance and interaction from the front—we also need to pay attention to how we talk to the back. Optimizing how we make requests is essential for performance, because we can create bottlenecks, increase response delay, and overload the server.

What we can do:

code example 4

From the front we can do:

code example 5

And from the back, if we have access and can modify it:

code example 6

Tools to measure and improve

Now that you know what to do, you need tools to tell whether you’re doing it right. My favorites:

Conclusion

Frontend performance isn’t just a technical topic. If your site isn’t fast, you lose users, money, and opportunities. The good news is that small changes can make a big difference.

My final advice: don’t try to do everything at once. Start with one change. Optimize those heavy images or remove unnecessary scripts.


Originally published on DEV Community.