Sub topic 1
Measure first: why profile before changing code
Performance work in Angular often starts with a guess. You see a slow screen, remember that OnPush helps, and add it everywhere. Sometimes this works. Often it changes nothing, or it adds bugs. This chapter explains why you should measure before you change code, and what a useful measurement looks like.
Prerequisites
This chapter assumes you already know the following:
- Basic Angular: components, templates, and services.
- TypeScript and the browser event loop.
- How to run an Angular app locally with the Angular CLI.
- How to open panels and the Console in Chrome DevTools.
Why guessing fails
A browser runs your JavaScript, style calculation, layout, and painting on one main thread. A slow interaction can come from any of these. It can also come from the network, from a large bundle, or from a third-party script. Angular change detection is only one possible cause.
If you optimize the wrong part, you pay three costs:
- You spend time on code that was not slow.
- You make the code harder to read, for example with manual caching or extra
OnPushhandling. - You may still not know whether the real problem is fixed.
A profile is a recording of what the browser did over a period of time. It shows where the time went, so you pick the target from evidence.
A measurement loop
Use the same loop each time:
- Describe the problem as something you can observe. "The list feels slow" is vague. "Typing in the search box takes about 400 ms to update the list" is specific.
- Reproduce it the same way every time, with the same data and the same steps.
- Record a baseline with a profiler.
- Form one hypothesis from what the recording shows.
- Make one change.
- Record again and compare with the baseline.
What counts as a good measurement
A single number from one run is weak evidence. Timings vary between runs because of caching, other tabs, and background work on your machine. Good measurements share these traits:
- They are repeatable. You can run the same steps again and get a similar result.
- They are taken in realistic conditions. Later chapters cover production builds and throttling, because a development build on a fast laptop can hide or exaggerate problems.
- They have a clear unit, such as milliseconds for an interaction or kilobytes for a bundle.
An example
Suppose a table of 2,000 rows feels slow when you filter it. You suspect the filter code. Instead of rewriting it, you first time it. The browser's performance.mark and performance.measure functions label a span of code in a recording:
filterItems(term: string) {
performance.mark('filter-start');
const result = this.items.filter(i => i.name.includes(term));
performance.measure('filter', 'filter-start');
return result;
}The recording might show filter taking 2 ms, while rendering the rows takes 300 ms. Your guess was wrong. The real cost is in rendering, so the fix belongs there. Without the measurement, you would have optimized code that took under 1% of the time.
What comes next
The next chapters set up realistic conditions, then walk through recording and reading a trace in the Performance panel. Each optimization later in the tutorial is paired with a before-and-after measurement.
Exercise
A teammate says the product page is slow and wants to add ChangeDetectionStrategy.OnPush to every component right away. What should you do first, and how will you know the change helped?
Show answer
First define the slowness in observable terms, such as "the page takes 3 seconds to respond after a click", and reproduce it with fixed steps. Record a baseline in the Performance panel and check where the time goes. Only then change code, ideally one change at a time. Record again with the same steps and compare the numbers with the baseline. If the time did not drop, the guess was wrong.