sourceTextModule.createCachedData()
- 返回:<Buffer>
创建一个可与 SourceTextModule 构造函数的 cachedData 选项一起使用的代码缓存。返回一个 Buffer。在模块被执行之前,此方法可以调用任意次。
【Creates a code cache that can be used with the SourceTextModule constructor's
cachedData option. Returns a Buffer. This method may be called any number
of times before the module has been evaluated.】
SourceTextModule 的代码缓存不包含任何 JavaScript 可观察状态。代码缓存可以安全地与脚本源一起保存,并可用于多次构建新的 SourceTextModule 实例。
【The code cache of the SourceTextModule doesn't contain any JavaScript
observable states. The code cache is safe to be saved along side the script
source and used to construct new SourceTextModule instances multiple times.】
SourceTextModule 源中的函数可以标记为延迟编译,它们在 SourceTextModule 构建时不会被编译。这些函数将在第一次被调用时编译。代码缓存会序列化 V8 当前已知的关于 SourceTextModule 的元数据,这些元数据可以用来加速未来的编译。
【Functions in the SourceTextModule source can be marked as lazily compiled
and they are not compiled at construction of the SourceTextModule. These
functions are going to be compiled when they are invoked the first time. The
code cache serializes the metadata that V8 currently knows about the
SourceTextModule that it can use to speed up future compilations.】
// Create an initial module
const module = new vm.SourceTextModule('const a = 1;');
// Create cached data from this module
const cachedData = module.createCachedData();
// Create a new module using the cached data. The code must be the same.
const module2 = new vm.SourceTextModule('const a = 1;', { cachedData });