sourceTextModule.createCachedData()
- 返回: <Buffer>
创建可与 SourceTextModule
构造函数的 cachedData
选项一起使用的代码缓存。
返回 Buffer
。
在评估模块之前,可以多次调用此方法。
SourceTextModule
的代码缓存不包含任何 JavaScript 可观察状态。
代码缓存可以安全地与脚本源一起保存,并用于多次构造新的 SourceTextModule
实例。
SourceTextModule
源代码中的函数可以标记为延迟编译,并且在构建 SourceTextModule
时不会编译它们。
这些函数将在第一次调用时被编译。
代码缓存序列化 V8 目前知道的关于 SourceTextModule
的元数据,它可以用来加速未来的编译。
// 创建初始模块
const module = new vm.SourceTextModule('const a = 1;');
// 从这个模块创建缓存数据
const cachedData = module.createCachedData();
// 使用缓存数据创建新的模块。代码必须相同。
const module2 = new vm.SourceTextModule('const a = 1;', { cachedData });
- Returns: <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.
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.
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 });