Sub topic 17
Verifying an optimization with before-and-after traces
This chapter assumes you can record a trace in the Performance panel, read the flame chart, and apply OnPush, signals, and track. The goal here is to confirm that a change helped, using numbers you can repeat.
Why one trace is not proof
A single recording varies from run to run. Garbage collection, browser extensions, and background tabs all change timings. If you record once before and once after, a difference of 10% may be noise. Treat a result as real only when it is larger than the spread between repeated runs of the same build.
Fix the conditions
Change one thing at a time, and keep everything else the same.
- Use the same production-like build and source maps for both runs.
- Use the same CPU and network throttling settings.
- Use the same data and the same user steps, in the same order.
- Use an Incognito window with extensions disabled.
- Reload the page before each recording so the app starts from a clean state.
Write the steps down. For example: "Load the page, click Filter, type shoe, stop recording after the list updates."
Choose a metric before you record
Pick a number that matches the problem you set out to fix. Good choices are:
- The duration of a long task, from the task bar in the Main track.
- The duration of your own
performance.measureentry in the Timings track. - The number of change detection passes, from Angular DevTools.
- Scripting time in the Summary tab for the selected range.
Recording the metric first prevents you from picking a flattering number after the fact.
Run the comparison
- Record the baseline at least three times. Note the metric each time.
- Apply one change.
- Record the same scenario at least three times.
- Compare the medians, not the best runs.
A performance.measure entry makes this easy, because the label and duration appear in every trace.
filterItems(term: string) {
performance.mark('filter-start');
const result = this.items.filter(i => i.name.includes(term));
performance.measure('filter', 'filter-start');
return result;
}You can also read the result in the Console without opening a trace.
const [entry] = performance.getEntriesByName('filter').slice(-1);
console.log(entry.duration.toFixed(1), 'ms');Record the result
A small table keeps the comparison honest.
| Run | Before (ms) | After (ms) |
|---|---|---|
| 1 | 182 | 41 |
| 2 | 176 | 38 |
| 3 | 190 | 44 |
| Median | 182 | 41 |
Here the change cut the median from 182 ms to 41 ms. The spread within each column is about 14 ms and 6 ms, which is much smaller than the 141 ms gap, so the improvement is real.
Check for side effects
A faster number can hide a regression elsewhere. After the change, confirm that:
- The view still shows the correct data. OnPush and signals can leave stale output if an input is mutated in place.
- No new long tasks appeared in other interactions.
- Memory in the Memory panel does not grow after repeating the action, which would point to a leak.
- Bundle size in the Network panel did not increase.
Exercise
Your baseline runs of a scroll handler take 60, 75, and 90 ms. After moving the handler outside the Angular zone, the runs take 66, 70, and 80 ms. Did the change help?
Show answer
The data does not show it. The medians are 75 ms and 70 ms, a 5 ms gap, while each set of runs varies by about 30 ms and 14 ms. The gap is smaller than the spread, so it is likely noise. Record more runs, or pick a metric closer to the problem, such as the number of change detection passes during scrolling.
Exercise
Name three conditions you must keep identical between the before and after recordings.
Show answer
Any three of: the production-like build, the throttling settings, the data, the user steps, and a fresh page load in a clean browser profile.