# Injection on demand and when necessary
In addition to package downloads, code injection is a major time-consuming part of the Weixin Mini Program startup process.The size of the amount of injected code is positively related to memory usage and injection time.
The features of to inject on demand" and to usefully inject can optimize code injection time and memory usage.
# Injection on demand
Base library 2.11.1 and above is supported, and below 2.11.1 is compatible but not optimized. For tool debugging, use version 1.05.2111300 and above, and base library version 2.20.1 and above.
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.
As of base library version 2.11.1, Weixin Mini Program supports the selective injection of necessary code through configuration to reduce the startup time and runtime memory of Mini Programs.
{
"lazyCodeLoading": "requiredComponents"
}
# Note
- With on-demand injection enabled, Weixin Mini Program injects only the custom components and page code needed to access the current page.Unvisited pages, custom components that are not declared on the current page are not loaded and initialized, and the corresponding code files are not executed. Please make sure that the Mini Program performs properly after modifying the configuration .
- With on-demand injection enabled, all components defined in the page JSON configuration and
apagejson]]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. - Plug-in packages and extension libraries do not currently support on-demand injection.If you need to implement plug-in loading on demand, consider placing the plug-in in a sub-package and introducing **asynchronously in the form of](../subpackages/async.md) by sub-package asynchronization.
# Inject during use
The base library version 2.11.2 and above is supported, and the effect below 2.11.2 is the same as unconfigured. For tool debugging, use version 1.05.2111300 and above, and base library version 2.20.1 and above.
With the on-demand injection feature turned on, time injection can specify that a portion of a custom component is not injected when Weixin Mini Program starts, but only when it is actually rendered.
After having specifiedlazyCodeLoadingIn the case ofrequiredComponents, configure to occupy a custom component and the component is automatically considered a time-injected component:
- On each page, the component is not injected until the first time it is rendered;
- On each page, the component is rendered as its corresponding occupied component the first time it is rendered, and injection begins when the rendering process is finished;
- After the injection is finished, the occupied component is replaced with a corresponding component.