Sub topic 10
Labeling your own code with User Timing
This chapter assumes you can record a trace in the Performance panel and read the flame chart. It also assumes you know basic Angular components and TypeScript.
Why label your code
A flame chart shows function names, but production builds minify them, and your own logic is often buried among framework calls. The User Timing API lets you put named labels into the trace. You can then find your code by name instead of guessing from stack shapes.
The API is part of the browser, not Angular. It has two calls:
performance.mark(name)records a named timestamp.performance.measure(name, startMark)records a named span from that mark to now.
Chrome shows each measure in the Timings track of a recording.
Worked example
This component wraps a list filter in a mark and a measure.
import { Component } from '@angular/core';
@Component({
selector: 'app-item-filter',
template: `
<input (input)="onInput($any($event.target).value)" />
<p>{{ shown.length }} items</p>
`,
})
export class ItemFilterComponent {
items = Array.from({ length: 50000 }, (_, i) => ({ name: `item-${i}` }));
shown = this.items;
onInput(term: string) {
this.shown = this.filterItems(term);
}
filterItems(term: string) {
performance.mark('filter-start');
const result = this.items.filter(i => i.name.includes(term));
performance.measure('filter', 'filter-start');
return result;
}
}To use it:
- Run a production-like build and open the app.
- Start a recording in the Performance panel.
- Type in the input, then stop the recording.
- Open the Timings track and look for the bar named
filter.
The filter bar sits inside a longer event task. Compare their widths. If filter is a small part of the task, your code is not the main cost. The rest is probably change detection or rendering that follows the handler.
Common errors
- Missing start mark.
performance.measurethrows aSyntaxErrorif the named mark does not exist. A typo in the mark name is the usual cause. - Early returns and exceptions. If the code between the calls can throw, put the
measurecall in afinallyblock so the span is still recorded. - Growing buffers. Marks and measures stay in memory until you clear them. In code that runs often, call
performance.clearMarks('filter-start')andperformance.clearMeasures('filter')after measuring. - Measuring too little. The span covers only your synchronous code. It does not include the change detection pass Angular runs afterward. To see that cost, look at the rest of the task in the flame chart.
filterItems(term: string) {
performance.mark('filter-start');
try {
return this.items.filter(i => i.name.includes(term));
} finally {
performance.measure('filter', 'filter-start');
performance.clearMarks('filter-start');
}
}Exercise
A recording shows a filter bar of 4 ms inside a 60 ms click task. What does this tell you about where to look next, and why might performance.measure throw an error in your code?
Show answer
Your filter code accounts for only about 4 ms of the 60 ms task, so the remaining time is elsewhere, likely change detection or rendering after the handler. Look at the flame chart for the rest of the task. The measure call throws a SyntaxError when the start mark name does not exist, for example because of a typo or because the mark was never created on that code path.