Sub topic 2
Spotting Long Tasks That Block the Main Thread
Assume you know Angular components, templates, TypeScript, the browser event loop, and basic Chrome DevTools. This chapter connects a Performance trace to Angular work.
Spotting long tasks
A task is work the browser performs on the main thread. JavaScript, layout, style calculation, and painting can all run there. A long task lasts more than 50 milliseconds. While it runs, the browser cannot respond promptly to clicks, scrolling, or typing.
Open Chrome DevTools, select Performance, and record a realistic interaction. Stop the recording and inspect the Main track. Long blocks, often marked with red triangles, identify work that may cause visible delay.
How Zone.js triggers Angular work
Zone.js patches browser APIs such as timers and event listeners. When an asynchronous callback finishes, Zone.js can notify Angular that application work occurred. Angular may then run change detection across part of the component tree.
In a trace, look above and below an event such as click, setTimeout, or fetch completion. The callback may lead to Angular framework calls and template evaluation. Applications using Angular’s zoneless mode will have a different notification path.
Recognizing change detection in a trace
Expand the long task and look for repeated Angular calls, template functions, or many component updates. Names vary between Angular versions and build modes, so interpret the whole stack rather than searching for one exact function.
Angular change detection may be expensive when a template calls methods repeatedly, recalculates large collections, or renders too many rows. A single slow component can also appear many times if an event causes the tree to update repeatedly.
Profiling components with Angular DevTools
Install Angular DevTools, open its Profiler tab, and record the same interaction. The profiler shows change-detection cycles and components that took the most time. Select a cycle to inspect components with high duration or frequent updates. Compare those components with the corresponding Main-thread activity in Chrome.
Labeling your own code with User Timing
The User Timing API adds named markers to a Performance trace.
function filterItems(items: { name: string }[], term: string) {
performance.mark('filter-start');
const result = items.filter(item => item.name.includes(term));
performance.measure('filter', 'filter-start');
return result;
}Find filter in the Performance panel’s timings track, then inspect the work between its marks.
Exercise
A click produces a 120 ms task, and Angular DevTools shows one component dominating the cycle. What should you inspect next?
Show answer
Expand the task and component details, then check that component’s template expressions, list rendering, and event handlers before changing code.