Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: Fix a Slow Page

A hands-on project. Audit a deliberately slow landing page, find five problems, apply the fixes step by step and compare the before and after results.

Time to put the chapters together. In this project you will take a deliberately slow landing page, find what is wrong with it, fix each problem and prove the improvement. The page is simulated in the preview so everything runs in your browser, and every problem corresponds to a real-world mistake from earlier lessons.

The plan

flowchart LR
  A["1. Measure<br/>the baseline"] --> B["2. Find problems"] --> C["3. Fix one"] --> D["4. Measure again"] --> E{"Better?"}
  E -->|"yes"| C2["Keep it, pick the next"]
  E -->|"no"| R["Revert"]

Change one thing at a time, and keep only changes that measurably help.

The slow page

The page has five deliberate problems. Read the code first and try to list the issues yourself before reading further.

The slow landing page (before)

Audit: the five problems

  1. Long task at startup (slowStartup): 250 ms of blocking work before the page can respond.
  2. Layout thrashing: writing style.height and reading offsetWidth for each of 1,200 items.
  3. Layout shift: the banner appears late and pushes everything down, so CLS goes up.
  4. Too much DOM work up front: 1,200 nodes rendered even though only a few are visible.
  5. Everything blocks the first render: the script is inline and synchronous, so nothing paints until all of it finishes.

The fixed version

Each change below matches a lesson:

  • Move the heavy startup work out of the first render (defer it, chunk it, or run it when idle, as in the main thread lesson).
  • Read once, write many (layout thrashing).
  • Reserve space for the banner so nothing shifts.
  • Let the browser skip offscreen items with content-visibility (containment).
  • Build the grid in a fragment and in a single insertion.
The fixed landing page (after)

Compare the "Setup took" lines of both versions. The number will differ with your device, but the second one should be far smaller because the page no longer blocks on the heavy work and no longer thrashes layout.

Measure the layout shift yourself

You can compute a layout shift score with PerformanceObserver. In a normal tab, paste this into the console while loading each version:

JavaScript
let cls = 0;
new PerformanceObserver((list) => {
  for (const e of list.getEntries()) if (!e.hadRecentInput) cls += e.value;
  console.log("CLS so far:", cls.toFixed(3));
}).observe({ type: "layout-shift", buffered: true });

Record the result

Make a small results table, like a real report:

JS Before and after report

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The numbers in this report are example values; yours will come from your own measurements.

Extension ideas

  • Add a hero image and give it width, height and fetchpriority="high".
  • Load a "reviews" widget only when it scrolls into view with IntersectionObserver.
  • Add a performance budget (a script that fails when the page has more than 1,000 nodes).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which fix removes the layout shift caused by a late banner?

    Reserved space means the banner fills an existing area instead of pushing content.
  2. 2. Why was reading offsetWidth inside the loop a problem?

    Alternating writes and reads forces synchronous layouts.
  3. 3. What is the right way to evaluate whether a fix worked?

    Only comparable measurements prove an improvement.
  4. 4. How does content-visibility auto help this page?

    The browser avoids rendering items far below the viewport.

Summary

  • Audit by reading the code and measuring; list the problems before fixing.
  • Change one thing at a time and keep what measurably helps.
  • The typical culprits are long tasks, layout thrashing, late-injected content and rendering work nobody sees.
  • Report before and after numbers like a real performance review.

Next: Project: A Tiny Real-User Monitor