Sub topic 3
Recording a trace in the Performance panel
This chapter assumes you can run an Angular app with the Angular CLI, know how the browser event loop works, and can open Chrome DevTools panels. It covers how to record a trace, what the recording contains, and the mistakes that make a trace misleading. Reading the flame chart itself comes in a later chapter.
What a trace is
A trace is a time-ordered log of what the browser did while you were recording. It includes JavaScript calls, style recalculation, layout, paint, network activity, and frame timing. The Performance panel in Chrome DevTools collects this log and draws it as a set of tracks.
A trace answers questions such as "what was the main thread doing between the click and the screen update?" It does not tell you why the code is slow. It tells you where the time went, and that tells you where to look.
Prepare the recording environment
Most bad traces come from a bad setup. Before you record, do the following.
- Use a production-like build, as described in the earlier chapter, so that development-mode checks do not distort the numbers.
- Open the app in an Incognito window, where extensions are normally disabled. Extensions add their own scripts and can show up in your trace.
- Close other tabs that do heavy work.
- Open DevTools and select the Performance tab.
- Decide which single interaction you want to measure, for example "type in the search box" or "open the orders page".
Recording one interaction at a time keeps the trace small enough to read. A five-minute session with many actions is hard to interpret.
Two ways to record
The panel has two main recording modes. They suit different problems.
| Mode | How to start | Best for |
|---|---|---|
| Record | Click the record button, act, then click stop | Runtime interactions such as clicks, typing, and scrolling |
| Record and reload | Click the reload button in the panel | Page load and Angular bootstrap |
The record button starts capturing immediately. You then perform your interaction and stop. The reload button refreshes the page, starts recording, and stops a few seconds after the load finishes.
A useful routine for runtime work is:
- Load the page and let it settle.
- Click record.
- Perform the interaction once, slowly and deliberately.
- Wait for the screen to finish updating.
- Click stop.
Waiting before you stop matters. Work that Angular schedules after your event, such as a follow-up change detection pass or a timer, only appears if you keep recording until the app is idle.
The recording options
Above the tracks, the panel offers settings that change what gets captured. The names can vary slightly between Chrome versions, but these are the ones that matter for Angular work.
- Screenshots captures a filmstrip of the page. It lets you match a point in the trace to what the user saw. It adds a little overhead, so leave it on for exploration and consider turning it off for fine measurement.
- Memory records JavaScript heap size over time. Turn it on when you suspect leaks. Leave it off otherwise, because it adds overhead.
- CPU throttling and Network throttling slow the machine and connection to mimic real devices. The next chapter covers these.
- Enable advanced paint instrumentation records detail about painting. You rarely need it for Angular change detection problems.
What the panel shows
After you stop, the panel displays several areas.
- The overview at the top is a timeline of the whole recording. It shows frames per second, CPU activity, and network activity. Drag across it to select the part you want to inspect.
- The tracks below it show what happened at each moment. The Main track holds the main thread's call stacks. The Frames track shows when frames were produced. The Timings track shows User Timing marks and measures, which a later chapter covers. The Network track shows requests.
- The details pane at the bottom has tabs such as Summary, Bottom-Up, Call Tree, and Event Log. Select a section of the timeline and these tabs summarize it.
The Summary tab is a good first stop. It splits the selected range into categories such as Scripting, Rendering, Painting, and Idle. If Scripting dominates, your JavaScript is the cost. If Rendering dominates, the browser's style and layout work is the cost, often because of what your code changes in the DOM.
Selecting a range
The whole trace usually contains noise from before and after your interaction. Select only the part you care about. Click and drag in the overview, or click and drag in the track area. The details pane then reports numbers for that range only.
Selecting a range also protects your comparisons. If you compare "the 400 ms after the click" in two traces, you are comparing like with like. If you compare whole recordings, differences in how long you waited before stopping will skew the totals.
Add your own markers
The default trace shows browser and framework activity, but not the names of your own operations. Marking them makes them easy to find. The next code uses performance.mark and performance.measure, and the result appears in the Timings track.
filterItems(term: string) {
performance.mark('filter-start');
const result = this.items.filter(i => i.name.includes(term));
performance.measure('filter', 'filter-start');
return result;
}When you record and run this method, a labeled filter entry appears in the Timings track. You can click it to see its duration, and you can line it up with the call stacks on the Main track. The later chapter on User Timing covers this in depth.
Common mistakes
These problems account for many misleading traces.
- Profiling
ng serveoutput. Development builds include extra checks and are not optimized. Your numbers will be pessimistic and will point at costs users never pay. - Recording too much. A long recording hides the interaction you care about. Record one action.
- Stopping too early. Follow-up work is cut off, so you miss the second change detection pass or a delayed timer.
- Measuring the first run only. The first run includes cold caches and just-in-time compilation. Repeat the interaction a few times and look at the later runs, or record a warm run after one practice pass.
- Changing conditions between traces. Different throttling, different tabs open, or a laptop switching between battery and power can all change timings. Keep conditions the same.
- Trusting one trace. Timings vary. Record several traces and compare typical values, not a single best or worst case.
Saving and sharing a trace
You can save a recording with the download button in the Performance panel. The file is a JSON trace that you can load again later with the import button. This is useful for two reasons.
- You keep a "before" trace to compare against after you make a change.
- You can share a trace with a teammate, who can open it without running your app.
Name saved files so you can tell them apart, for example search-before.json and search-after.json. A trace can contain URLs and other page details, so check it before sharing it outside your team.
A short worked pass
Suppose typing in a search box feels slow. A recording pass might go like this.
- Build in production mode and serve it locally.
- Open the page in Incognito and open the Performance panel.
- Click record, type three characters into the box, wait one second, and click stop.
- Drag across the overview to select the three typing bursts.
- Open the Summary tab and note the Scripting and Rendering totals.
- Save the trace as
search-before.json.
You now have a baseline. The later chapters show how to read the Main track, find the change detection work, and make a change. When you finish, you record again under the same conditions and compare against this file.
Exercise
You record a trace of a button click in ng serve with every capture option enabled, then record another trace after a code change in a production build with Screenshots turned off. The second trace is faster. Why can you not conclude that your code change helped, and what would you do instead?
Show answer
The two recordings differ in build type and capture options as well as in the code change, so any of these could explain the speedup. Development builds run extra checks, and each capture option adds overhead. Record both the before and after traces in the same production-like build, with the same options and throttling, and compare the same selected range of the timeline across several runs.
Exercise
You click record, click a button, and click stop immediately. The trace shows the click handler but no second change detection pass that you expected after a timer fired. What is the likely cause, and how do you fix it?
Show answer
You stopped recording before the delayed work ran, so it was never captured. Keep recording after the interaction until the app is idle and the screen has finished updating, then click stop.