@@ -126,6 +126,16 @@ Creates a code cache that can be used with the `Script` constructor's
126126` cachedData ` option. Returns a ` Buffer ` . This method may be called at any
127127time and any number of times.
128128
129+ The code cache of the ` Script ` doesn't contain any JavaScript observable
130+ states. The code cache is safe to be saved along side the script source and
131+ used to construct new ` Script ` instances multiple times.
132+
133+ Functions in the ` Script ` source can be marked as lazily compiled and they are
134+ not compiled at construction of the ` Script ` . These functions are going to be
135+ compiled when they are invoked the first time. The code cache serializes the
136+ metadata that V8 currently knows about the ` Script ` that it can use to speed up
137+ future compilations.
138+
129139``` js
130140const script = new vm.Script (`
131141function add(a, b) {
@@ -135,11 +145,14 @@ function add(a, b) {
135145const x = add(1, 2);
136146` );
137147
138- const cacheWithoutX = script .createCachedData ();
148+ const cacheWithoutAdd = script .createCachedData ();
149+ // In `cacheWithoutAdd` the function `add()` is marked for full compilation
150+ // upon invocation.
139151
140152script .runInThisContext ();
141153
142- const cacheWithX = script .createCachedData ();
154+ const cacheWithAdd = script .createCachedData ();
155+ // `cacheWithAdd` contains fully compiled function `add()`.
143156```
144157
145158### ` script.runInContext(contextifiedObject[, options]) `
@@ -780,6 +793,15 @@ Creates a code cache that can be used with the `SourceTextModule` constructor's
780793` cachedData` option . Returns a ` Buffer` . This method may be called any number
781794of times before the module has been evaluated.
782795
796+ The code cache of the ` SourceTextModule` doesn' t contain any JavaScript
797+ observable states. The code cache is safe to be saved along side the script
798+ source and used to construct new `SourceTextModule` instances multiple times.
799+
800+ Functions in the `SourceTextModule` source can be marked as lazily compiled and
801+ they are not compiled at construction of the `SourceTextModule`. These
802+ functions are forced to be compiled when they are invoked the first time. The
803+ code cache serializes the current compilation state.
804+
783805```js
784806// Create an initial module
785807const module = new vm.SourceTextModule(' const a = 1 ;' );
0 commit comments