Sub topic 1
Measure First: Why Profile Before Changing Code
Prerequisites
This chapter assumes you know Angular components, templates, and services. You can run an Angular application with the CLI and use basic Chrome DevTools. You should also understand that browser work runs on the main thread unless moved elsewhere.
Measure Before You Change Code
Performance work starts with a measurement. A slow screen may come from Angular change detection, rendering a large list, JavaScript outside Angular, network delays, or a large bundle. A guess can lead to an optimization that adds complexity without improving the user experience.
Choose one repeatable action, such as opening a page and filtering a list. Record the conditions:
- The browser and device or emulator
- The data size
- The action being measured
- The time to complete the action
- Any visible delay or dropped frames
Repeat the same action after each change. Compare measurements instead of relying on how the page feels.
Use a Production-Like Build
Development builds include extra checks and diagnostic work. They can make Angular code appear slower than it is for users. Profile a production-like build with optimizations enabled. Keep source maps enabled when possible so DevTools can connect minified code to your TypeScript sources.
Do not compare a development trace before an optimization with a production trace afterward. Keep the build type, browser settings, data, and action consistent.
Record a Trace
Open Chrome DevTools, select Performance, and start a recording. Perform only the chosen action, then stop the recording. Avoid unrelated clicks because they add noise.
Simulate Realistic Conditions
A fast developer laptop may hide problems. Use CPU throttling to approximate the target device. Use network throttling to expose slow startup, API requests, and bundle downloads. Apply the same throttling settings to both the before and after recordings.
Read the Flame Chart
The flame chart shows main-thread activity over time. Time runs from left to right. Wider blocks took longer. Nested blocks show work called by their parent.
Look for long tasks, which are main-thread tasks that commonly block input and rendering when they run for more than about 50 milliseconds. Expand a wide block and inspect its JavaScript, rendering, and Angular-related work. Record the widest relevant block and its duration. That finding becomes your optimization target.
Exercise
Why should the before and after recordings use the same build type and throttling settings?
Show answer
Otherwise, build differences or device simulation can change the result, so you cannot attribute the improvement to your code change.