@@ -126,6 +126,16 @@ Creates a code cache that can be used with the `Script` constructor's
126
126
` cachedData ` option. Returns a ` Buffer ` . This method may be called at any
127
127
time and any number of times.
128
128
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
+
129
139
``` js
130
140
const script = new vm.Script (`
131
141
function add(a, b) {
@@ -135,11 +145,14 @@ function add(a, b) {
135
145
const x = add(1, 2);
136
146
` );
137
147
138
- const cacheWithoutX = script .createCachedData ();
148
+ const cacheWithoutAdd = script .createCachedData ();
149
+ // In `cacheWithoutAdd` the function `add()` is marked for full compilation
150
+ // upon invocation.
139
151
140
152
script .runInThisContext ();
141
153
142
- const cacheWithX = script .createCachedData ();
154
+ const cacheWithAdd = script .createCachedData ();
155
+ // `cacheWithAdd` contains fully compiled function `add()`.
143
156
```
144
157
145
158
### ` script.runInContext(contextifiedObject[, options]) `
@@ -780,6 +793,16 @@ Creates a code cache that can be used with the `SourceTextModule` constructor's
780
793
` cachedData` option . Returns a ` Buffer` . This method may be called any number
781
794
of times before the module has been evaluated.
782
795
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
801
+ and they are not compiled at construction of the `SourceTextModule`. These
802
+ functions are going to be compiled when they are invoked the first time. The
803
+ code cache serializes the metadata that V8 currently knows about the
804
+ `SourceTextModule` that it can use to speed up future compilations.
805
+
783
806
```js
784
807
// Create an initial module
785
808
const module = new vm.SourceTextModule(' const a = 1 ;' );
0 commit comments