Skip to article content
FrontendXLesson 09 / 20

Performance regressions

Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.

advanced · about 45 minutes · 2 exercises

  • Explain
  • Debug

What you will be able to do

#
  • Establish a measured baseline before changing performance code.
  • Recognise repeated work, layout thrashing and leaked listeners.

Overview

#

Performance bugs are invisible in a single run and obvious across thousands. Measure before you change anything, or you will optimise the wrong line.

Start with a baseline number. A profiler shows where time is actually spent; intuition is usually wrong. Record the number, make one change, record it again.

The first common shape is repeated work in a loop, often a query or a layout read inside an iteration.

snippet.js
function render(rows) {
  let html = '';
  rows.forEach((row) => {
    html += `<li>${row.label}</li>`;
  });
  list.innerHTML = html;
}

For large lists, repeated string concatenation and full re-renders are expensive. Batch the work and write once.

snippet.js
function render(rows) {
  const html = rows.map((row) => `<li>${row.label}</li>`).join('');
  list.innerHTML = html;
}

The second shape is layout thrashing: reading a layout property such as offsetWidth forces the browser to recalculate layout, and doing it inside a write loop repeats that cost.

snippet.js
// thrash: read, write, read, write
items.forEach((el) => {
  el.style.width = el.offsetWidth + 10 + 'px';
});

Read all values first, then write all values, so the browser can batch the layout.

The third shape is a leak. A listener, timer or subscription that is never removed keeps its closure, and its data, alive. In a long-lived page the memory grows until it shows up as jank or a crash. The fix is symmetric cleanup: every addEventListener gets a matching removeEventListener.

Always confirm the fix with the same measurement you used for the baseline. A change that feels faster is not evidence.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Debug

    This loop reads offsetWidth inside a write loop, forcing a layout recalculation on every iteration. Fix it so all reads happen before all writes.

    starter.javascript
    items.forEach((el) => {
      el.style.width = el.offsetWidth + 10 + 'px';
    });

    Reveal the reference answer
    reference.javascript
    const widths = items.map((el) => el.offsetWidth);
    items.forEach((el, i) => {
      el.style.width = widths[i] + 10 + 'px';
    });

    Authored expected output: All widths are read first into an array, then all writes happen, so the browser performs one layout pass instead of one per item.

    Reading offsetWidth after a style write invalidates the layout and forces a synchronous recalculation. Separating the read pass from the write pass removes the thrash.

  2. Exercise 2Explain

    Explain why measuring before optimising matters, and the risk of optimising by intuition.

    Reveal the reference answer
    reference.text
    Without a baseline you cannot tell whether a change helped, and you may spend effort on code that is not the bottleneck. Profilers show where time is actually spent, which is often not where it feels slow. Optimising by intuition risks adding complexity for no measured gain and can even make the hot path slower.

    Authored expected output: An explanation that a baseline is required to prove a gain and that intuition often misidentifies the bottleneck.

    A baseline turns a feeling into a number you can compare after the change. Without it you cannot tell a real gain from noise, and you may add complexity to code that was never the bottleneck.

Common misconceptions

#
  • A change that feels faster is a proven optimisation.
  • Reading a layout property is free.

Follow-up practice

#
  • debugging-review-10-incident-review

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.