@@ -138,6 +138,16 @@ Creates a code cache that can be used with the `Script` constructor's
138
138
` cachedData ` option. Returns a ` Buffer ` . This method may be called at any
139
139
time and any number of times.
140
140
141
+ The code cache of the ` Script ` doesn't contain any JavaScript observable
142
+ states. The code cache is safe to be saved along side the script source and
143
+ used to construct new ` Script ` instances multiple times.
144
+
145
+ Functions in the ` Script ` source can be marked as lazily compiled and they are
146
+ not compiled at construction of the ` Script ` . These functions are going to be
147
+ compiled when they are invoked the first time. The code cache serializes the
148
+ metadata that V8 currently knows about the ` Script ` that it can use to speed up
149
+ future compilations.
150
+
141
151
``` js
142
152
const script = new vm.Script (`
143
153
function add(a, b) {
@@ -147,11 +157,14 @@ function add(a, b) {
147
157
const x = add(1, 2);
148
158
` );
149
159
150
- const cacheWithoutX = script .createCachedData ();
160
+ const cacheWithoutAdd = script .createCachedData ();
161
+ // In `cacheWithoutAdd` the function `add()` is marked for full compilation
162
+ // upon invocation.
151
163
152
164
script .runInThisContext ();
153
165
154
- const cacheWithX = script .createCachedData ();
166
+ const cacheWithAdd = script .createCachedData ();
167
+ // `cacheWithAdd` contains fully compiled function `add()`.
155
168
```
156
169
157
170
### ` script.runInContext(contextifiedObject[, options]) `
@@ -792,6 +805,16 @@ Creates a code cache that can be used with the `SourceTextModule` constructor's
792
805
` cachedData` option . Returns a ` Buffer` . This method may be called any number
793
806
of times before the module has been evaluated.
794
807
808
+ The code cache of the ` SourceTextModule` doesn' t contain any JavaScript
809
+ observable states. The code cache is safe to be saved along side the script
810
+ source and used to construct new `SourceTextModule` instances multiple times.
811
+
812
+ Functions in the `SourceTextModule` source can be marked as lazily compiled
813
+ and they are not compiled at construction of the `SourceTextModule`. These
814
+ functions are going to be compiled when they are invoked the first time. The
815
+ code cache serializes the metadata that V8 currently knows about the
816
+ `SourceTextModule` that it can use to speed up future compilations.
817
+
795
818
```js
796
819
// Create an initial module
797
820
const module = new vm.SourceTextModule(' const a = 1 ;' );
0 commit comments