# Lazy Code Loading and On-Demand Injection
During Mini-Program startup, besides downloading the code package, code injection is another major time-consuming step. The amount of code injected is directly proportional to memory usage and injection time.
By utilizing the features of Lazy Code Loading and On-Demand Injection, you can optimize the time consumption and memory usage during the code injection phase.
# 1. Lazy Code Loading
Supported in Base Library version 3.0.38 and above. Compatible with versions below 3.0.38 but without optimization effects. Use Developer Tools version 1.8.44 or above for debugging, and SDK version 2.41.0 or above.
Typically, when a Mini-Program starts, all JS code from all code packages (Main Package, Subpackage, Plugin Package, Extended Library, etc.) that the startup page depends on is merged and injected entirely. This includes other unvisited pages and unused custom components. Simultaneously, the JS code for all pages and custom components is executed immediately. This results in a lot of unused code being injected and executed in the Mini-Program runtime environment, affecting injection time and memory usage.
Starting from Base Library version 3.0.38, Mini-Programs support configuring selective injection of necessary code through settings to reduce startup time and runtime memory.
{
"lazyCodeLoading": "requiredComponents"
}
# 1.1 Precautions
- After enabling Lazy Code Loading, the Mini-Program only injects the custom components and page code required by the currently accessed page. Unvisited pages and custom components not declared on the current page will not be loaded or initialized, and their corresponding code files will not be executed. Developers must verify that the Mini-Program behaves normally after modifying this configuration.
- After enabling Lazy Code Loading, all components defined in the page's JSON configuration and global custom components configured in
usingComponentswithinapp.jsonare considered dependencies of the page and will be injected and loaded. It is recommended that developers promptly remove declarations of unused custom components from JSON files and avoid globally declaring custom components with low usage rates, as this may otherwise impact the effectiveness of Lazy Code Loading. - Plugin Packages and Extended Libraries currently do not support Lazy Code Loading.
# 2. On-Demand Injection
Supported in Base Library version 3.0.38 and above. Compatible with versions below 3.0.38 but without optimization effects. Use Developer Tools version 1.8.44 or above for debugging, and SDK version 2.41.0 or above.
Under the premise that the "Lazy Code Loading" feature is enabled, "On-Demand Injection" allows specifying that certain custom components are not injected during Mini-Program startup, but are instead injected only when they are actually rendered.
When lazyCodeLoading is already set to requiredComponents, configuring a Component Placeholder for a custom component will automatically designate that component as an On-Demand Injection component:
- Within each page, this component will not be injected before its first render.
- Within each page, during its first render, the component will be rendered as its corresponding placeholder component. Injection begins after the rendering process for that instance finishes.
- After injection is complete, the placeholder component is replaced by the actual component.