Data update methods (such as setData) are interfaces that are prone to causing performance issues in Mini Program development.
For details on data update methods, please refer to Advanced Data Update Methods.
The Data Update Strategy document describes the algorithmic principles behind data updates for your reference.
Factors Affecting Data Update Performance
The main factors affecting the performance of data update methods are:
- The total number of nodes in the Shadow tree of the component that invokes the data update method;
- The volume of data being updated.
Among these, the former has a greater impact than the latter.
Usage Recommendations
1. Reasonable Component Division to Avoid Excessive Shadow Tree Size
As described in Data Update Algorithm, for the primarily adopted data update algorithm, its performance is significantly and positively correlated with the total number of nodes and the complexity of nodes in the Shadow tree. Therefore, the most effective strategy is to divide components reasonably and control the size of the Shadow tree.
- ✅ When the number of nodes in the page or component Shadow tree is excessive, appropriately split the components to reduce the number of nodes in the current Shadow tree.
2. Controlling the Frequency of Data Updates
Each call to setData triggers the component framework to execute the data update algorithm once. Calling setData too frequently (at millisecond intervals) leads to the following consequences:
- The logic layer JS thread remains continuously busy, preventing it from responding normally to user interaction events and completing page transitions;
- The view layer JS thread remains constantly occupied, increasing the communication latency between the logic layer and the view layer, resulting in higher message delivery delays and noticeable rendering lag;
- The view layer cannot respond to user interactions in a timely manner, causing obvious jitters when users swipe the page, delayed feedback for operations, and preventing user interaction events from being promptly passed to the logic layer. Consequently, the logic layer also cannot timely convey the processing results of operations to the view layer.
Therefore, developers should note the following when calling data update methods such as setData:
- ✅ Call data update methods only when page content needs to be updated;
- ✅ Use techniques such as combined updates to merge consecutive
setDatacalls whenever possible; - ❌ Avoid unnecessary data update calls;
- ❌ Avoid continuous data update calls at excessively high frequencies;
- ❌ Avoid calling data update methods on every instance within page scrolling callbacks.
3. Reduce per-update overhead for unavoidable frequent updates
If frequent data updates cannot be avoided (e.g., flash sale countdowns), use the following approaches to reduce update overhead:
- ✅ Encapsulate the logic into an independent component, keep the Shadow tree as small as possible, and perform data updates within the component;
- ✅ Satisfy the conditions for binding mapping table updates in the Data Update Algorithm as much as possible to reduce overhead;
- ✅ Use the CSS contain property to limit the scope of layout, style, and paint calculations;
- ❌ Avoid high-frequency data update calls within large components.
4. data should only contain rendering-related data
When using the exparser component framework, data should only include data used on the interface. Using data update methods to update fields unrelated to rendering triggers additional rendering processes, increases communication between the logic layer and the view layer, and affects rendering performance.
- ✅ The
datafield of a page or component should be used to store data fields used in WXML; - ✅ Data indirectly related to page or component rendering can be set as pure data fields, and
observerscan be used to monitor changes; - ✅ Data unrelated to page or component rendering should be attached to non-
datafields, such asthis.userData = {userId: 'xxx'}, or used as local variables within theinitof Chaining format; - ❌ Avoid including data fields in
datathat are not used in WXML; - ❌ Avoid using
datafor data sharing between page or component methods; - ❌ Avoid abusing pure data fields to store data.
5. setData should only pass changed data
The volume of updated data affects the time spent on data copying and communication, increasing the overhead of page updates and causing update delays.
- ✅ Pass only the changed fields to
setData; - ✅ Use other advanced data update methods instead of
setData; - ❌ Do not lazily pass all data at once in
setData:this.setData(this.data).
6. Controlling Data Updates for Background Pages
Since the Mini Program logic layer operates on a single thread, data updates in background pages compete for the execution resources of the foreground page. Furthermore, users cannot perceive the rendering of background pages, resulting in unnecessary overhead. On certain platforms, the WebViews in the Mini Program rendering layer also share the same thread, meaning that rendering and logic execution in background pages can cause lag in the foreground page.
- ✅ Update operations after a page switches to the background should be avoided where possible, or delayed until the page becomes visible;
- ❌ Avoid high-frequency data updates after switching to the background, such as countdown updates.
Performance Analysis
Developers can obtain update performance statistics through the component's setUpdatePerformanceListener interface to analyze components that are causing performance bottlenecks.