Home > @infiniteobjects/core-library-node > Import > lazy
Provides a way to improve process startup times and delay configurations by lazy-loading imported modules.
Signature:
static lazy(moduleName: string, require: (id: string) => unknown): any;
Parameter | Type | Description |
---|---|---|
moduleName | string | |
require | (id: string) => unknown |
Returns:
any
This is a more structured wrapper for the import-lazy package. It enables you to replace an import like this:
import * as example from 'example'; // <-- 100ms load time
if (condition) {
example.doSomething();
}
…with a pattern like this:
const example: typeof import('example') = Import.lazy('example', require);
if (condition) {
example.doSomething(); // <-- 100ms load time occurs here, only if needed
}
The implementation relies on JavaScript’s Proxy
feature to intercept access to object members. Thus it will only work correctly with certain types of module exports. If a particular export isn’t well behaved, you may need to find (or introduce) some other module in your dependency graph to apply the optimization to.