跪拜 Guibai
← Back to the summary

How to Diagnose Frontend Apps That Get Slower the Longer They Run

From Flame Graphs to Code: How to Pinpoint the 'Slows Down Over Time' Frontend Performance Bottleneck

In daily frontend development, we often encounter a frustrating problem: a page is silky smooth when first opened, but as the user spends more time on it, it gradually becomes sluggish — scrolling is no longer fluid, and click responses become delayed. This phenomenon is usually related to the browser's main thread being occupied for long periods, memory leaks, or frequent unnecessary re-renders.

This article will use Chrome DevTools' Performance panel as the core tool, combined with real-world cases, to guide you step-by-step in mastering how to use flame graphs and the Call Tree to precisely locate and resolve these persistent performance issues.

1. Getting to Know the Performance Profiling Tool: The Performance Panel

To conduct performance profiling, you first need to understand our "microscope" — Chrome DevTools' Performance panel.

1. Core Functionality Overview

The Performance panel is primarily used to record and analyze various performance metrics of a webpage during runtime, including:

2. Basic Interpretation of the Flame Chart

In the flame chart of the Main thread, the horizontal axis represents the timeline, and the vertical axis represents the call stack (i.e., the nesting level of the code). Different colors represent different types of work:

2. Hands-On Practice: A Macro-to-Micro Investigation Process

Faced with a complex flame graph, how should we proceed? Here is a standard investigation process.

1. Macro-Level Location: Finding Long Tasks

The most direct cause of page jank is often Long Tasks. In the Main thread, we need to look for color blocks that are very long in duration and very wide. Typically, tasks exceeding 50ms are considered Long Tasks, and Chrome sometimes marks them with a red triangle warning icon in the upper right corner or above these long blocks. It is these long tasks that block the browser's main thread, preventing the page from responding to user interactions in time, thus causing the feeling of sluggishness.

2. Micro-Level Location: Call Tree and Bottom-Up

When we find a Long Task block that causes jank, we need to drill down into it to find the culprit. This is where the Summary, Bottom-up, or Call tree panels below come in handy.

3. Advanced Techniques and Insights: Four Strategies to Solve 'Slows Down Over Time'

For the phenomenon of "the page only gets slow after being used for a while," besides routine investigation, we need to combine some specific techniques.

1. Beware of Memory Leaks (Combined with the Memory Panel)

The most common reason a page slows down over time is memory leaks.

2. Pay Attention to Zone.js Callbacks (For Modern Frontend Frameworks)

During investigation, you might find an entry named globalZoneAwareCallback zone.js:1724:1 in the Call tree. zone.js is the core library used by the Angular framework to handle asynchronous operation contexts and is also common in modern frontend projects (especially those based on Webpack/Vite).

3. Debounce and Throttle (For High-Frequency Events)

If jank occurs during scrolling (scroll), window resizing (resize), or mouse movement (mousemove), it is usually because these events trigger at a very high frequency, causing the callback function to execute frequently.

4. Long Task Splitting and Avoiding Forced Synchronous Layout

4. Summary

Frontend performance optimization is a long-term battle, and the Performance panel is our most powerful weapon. By skillfully using flame graphs, call trees, and memory analysis tools, we can, like a doctor, see through the symptoms to the essence and precisely locate the code lesions causing page jank.

Remember, the core principle of performance optimization is: Measure first, then optimize, and finally verify. Do not guess blindly; let the data tell you where to start. I hope the sharing in this article can help you face the 'slows down over time' problem with confidence and skill.