# Code Injection Optimization

Weixin Mini Program Code injection optimization can be done from the perspective of optimizing code amounts and optimized execution time .

# 1. On-demand injection using

Recommended for all Weixin Mini Program

Typically, when Weixin Mini Program starts, all the code packages that the page depends on (the main package, All JS code from sub-packages, plugin packages, extension libraries, etc. is injected fully, including other unvisited pages and unused custom components, and all pages and custom components are executed immediately. This causes a lot of unused code to be injected into the Mini Program runtime environment, affecting injection time and memory usage.

Since base library version 2.11.1, you can avoid unnecessary code injection and execution by turning on the On-Demand Injection feature to reduce startup time and runtime memory of Weixin Mini Program.

{
  "lazyCodeLoading": "requiredComponents"
}

Note: With on-demand injection enabled, all components defined in the page JSON configuration andapagejson]]inusingComponents]]The global custom components configured are treated as page dependencies and injected and loaded.Developers are advised to remove declarations in JSON that do not use custom components and try to **to avoid declaring low-usage custom components globally, otherwise it may affect the effectiveness of on-demand injections.

# 2. Time injection using

When you turn on the above-mentioned On-Demand Injection feature, you can use the Time Injection feature to enable some custom components to be injected not at startup but when they are actually rendered, further reducing the startup and first screen time of Weixin Mini Program.

# 3. Reduce synchronization API calls during startup

In the Weixin Mini Program startup process, developer code is injected and sequentially executed synchronously.ApageonLaunch,ApageonShow,Page.onLoad,Page.onShow

During the Weixin Mini Program initializer code (content outside the Page, App definition) and several lifecycles associated with the above startup, you should minimize or not call the **synchronization API.Most synchronization APIs end withSync, but there are some exceptions, such asgetSystemInfo.

The synchronization API, while simpler to use, blocks the current JS thread and affects code execution. If not necessary, use the asynchronous API instead of synchronization whenever possible, and delay the synchronous API calls that are not necessary during the startup process until after the startup is complete.

Common APIs that developers tend to call too frequently at startup are:

# 3.1 getSystemInfo / getSystemInfoSync

For historical reasons, both interfaces are implemented synchronously. Because the getSystemInfo interface is loaded with too much content, a single call can take too long.

If not necessary, it is recommended that the developer cache the result of the call to avoid repeated calls. The should be invoked as many times as possible during startup.

Priority is recommended for using the broken up. getSystemSetting/getAppAuthorizeSetting/getDeviceInfo/getWindowInfo/getAppBaseInfoGet information on demand, or use the asynchronous version getSystemInfoAsync .

# 3.2 getStorageSync / setStorageSync

GetStorageSync / setStorageSync should be used only for persistent data storage, not for runtime data passing or global state management .Too much read and write storage during startup can also significantly affect the time spent on Weixin Mini Program code injection.

For simple data sharing, you can do this by adding global data objects to your app:

// app.js
App({
  globalData: { // 全局共享的数据
    userName: 'Wechat'
  }
})

// pages/index.js
const app = getApp()
Page({
  onLoad() {
    const { userName } = app.globalData
  }
})

# 4. Avoid initiating a process for complex calculations

During several lifecycles associated with Weixin Mini Program initializer code (content outside the Page, App definition) and startup, should avoid performing complex computational logic and .Complex operations will also block the current JS thread, affecting the startup time. It is recommended that complex operations be delayed until the startup is complete.