模块编译缓存


¥Module compile cache

可以使用 module.enableCompileCache() 方法或 NODE_COMPILE_CACHE=dir 环境变量启用模块编译缓存。启用后,每当 Node.js 编译 CommonJS 或 ECMAScript 模块时,它都会使用保留在指定目录中的磁盘上 V8 代码缓存 来加快编译速度。这可能会减慢模块图的第一次加载,但如果模块的内容不改变,同一模块图的后续加载可能会显着加速。

¥The module compile cache can be enabled either using the module.enableCompileCache() method or the NODE_COMPILE_CACHE=dir environment variable. After it is enabled, whenever Node.js compiles a CommonJS or a ECMAScript Module, it will use on-disk V8 code cache persisted in the specified directory to speed up the compilation. This may slow down the first load of a module graph, but subsequent loads of the same module graph may get a significant speedup if the contents of the modules do not change.

要清理磁盘上生成的编译缓存,只需删除缓存目录即可。下次使用同一目录进行编译缓存存储时,将重新创建缓存目录。为避免用旧的缓存填满磁盘,建议使用 os.tmpdir() 下的目录。如果通过调用 module.enableCompileCache() 启用编译缓存而不指定目录,则 Node.js 将使用 NODE_COMPILE_CACHE=dir 环境变量(如果已设置),否则默认为 path.join(os.tmpdir(), 'node-compile-cache')。要找到正在运行的 Node.js 实例使用的编译缓存目录,请使用 module.getCompileCacheDir()

¥To clean up the generated compile cache on disk, simply remove the cache directory. The cache directory will be recreated the next time the same directory is used for for compile cache storage. To avoid filling up the disk with stale cache, it is recommended to use a directory under the os.tmpdir(). If the compile cache is enabled by a call to module.enableCompileCache() without specifying the directory, Node.js will use the NODE_COMPILE_CACHE=dir environment variable if it's set, or defaults to path.join(os.tmpdir(), 'node-compile-cache') otherwise. To locate the compile cache directory used by a running Node.js instance, use module.getCompileCacheDir().

目前,当使用带有 V8 JavaScript 代码覆盖率 的编译缓存时,V8 收集的覆盖率在从代码缓存反序列化的函数中可能不太精确。建议在运行测试时关闭此功能以生成精确的覆盖范围。

¥Currently when using the compile cache with V8 JavaScript code coverage, the coverage being collected by V8 may be less precise in functions that are deserialized from the code cache. It's recommended to turn this off when running tests to generate precise coverage.

可以通过 NODE_DISABLE_COMPILE_CACHE=1 环境变量禁用已启用的模块编译缓存。当编译缓存导致意外或不良行为(例如,测试覆盖率较低)时,这可能很有用。

¥The enabled module compile cache can be disabled by the NODE_DISABLE_COMPILE_CACHE=1 environment variable. This can be useful when the compile cache leads to unexpected or undesired behaviors (e.g. less precise test coverage).

一个版本的 Node.js 生成的编译缓存不能被不同版本的 Node.js 重用。如果使用相同的基目录来持久化缓存,则不同版本的 Node.js 生成的缓存将单独存储,因此它们可以共存。

¥Compilation cache generated by one version of Node.js can not be reused by a different version of Node.js. Cache generated by different versions of Node.js will be stored separately if the same base directory is used to persist the cache, so they can co-exist.

目前,当启用编译缓存并重新加载模块时,代码缓存会立即从编译的代码中生成,但只有在 Node.js 实例即将退出时才会写入磁盘。这可能会发生变化。如果应用想要生成其他 Node.js 实例并让它们在父级退出之前共享缓存,可以使用 module.flushCompileCache() 方法确保将累积的代码缓存刷新到磁盘。

¥At the moment, when the compile cache is enabled and a module is loaded afresh, the code cache is generated from the compiled code immediately, but will only be written to disk when the Node.js instance is about to exit. This is subject to change. The module.flushCompileCache() method can be used to ensure the accumulated code cache is flushed to disk in case the application wants to spawn other Node.js instances and let them share the cache long before the parent exits.