Sub topic 4
Throttling the CPU and network to mimic real devices
This chapter assumes you can open the Performance panel and record a trace, and that you profile a production-like build. A developer laptop is far faster than most phones. A trace recorded on it can hide problems that users see.
Why throttle
Chrome DevTools can slow down your own machine so it behaves more like a typical device. Two settings matter:
- CPU throttling makes JavaScript, layout, and rendering run slower by a fixed factor.
- Network throttling limits bandwidth and adds latency to requests.
Throttling does not make a trace more accurate in an absolute sense. It makes long tasks and slow loads easier to see, and it makes before-and-after differences larger than the noise.
Set CPU throttling
- Open DevTools and select the Performance panel.
- Click the settings (gear) icon to show the capture settings.
- Choose a value from the CPU list: No throttling, 4x slowdown, 6x slowdown, or a higher option.
A 4x slowdown means the CPU work takes about four times longer than it would on your machine. Start with 4x for a mid-range phone and use 6x for a low-end phone. Chrome also offers a calibration option for your hardware, which is useful when you compare results between computers.
Set network throttling
In the same settings area, open the Network list. Choose a preset such as Fast 4G or Slow 4G, or define a custom profile with your own download speed, upload speed, and latency. You can also set throttling in the Network panel. Both panels use the same setting.
Network throttling matters most when you record a page load, because the app bundle must download before Angular can start.
Keep conditions the same
flowchart TDPick one combination and use it for every recording in a comparison. Changing the throttle between runs makes the numbers incomparable.
What changes in the trace
With 4x CPU throttling, a task that took 30 ms may take about 120 ms. The browser flags tasks over 50 ms as long tasks, so work that looked fine on your laptop now appears as a long task with a red marker in the Main track. Change detection passes, template method calls, and large list renders are common examples.
Common mistakes
- Comparing a throttled trace with an unthrottled one.
- Profiling
ng serveoutput with throttling. Development builds add checks that distort the results, so use a production build. - Recording once. Run each scenario at least three times, because the numbers vary from run to run.
- Treating the throttle factor as an exact device speed. It is an approximation.
Exercise
You record a click handler at 4x CPU slowdown and see a 140 ms task. What would you expect the same task to take with no throttling, roughly, and why might it still matter on a phone?
Show answer
About 35 ms (140 ms divided by 4). That is under the 50 ms long-task threshold on your machine, but on a mid-range phone it behaves like the throttled 140 ms task, which blocks input and rendering for a noticeable time.