Skip to content

Commit 10891e6

Browse files
legendecasRafaelGSS
authored andcommitted
doc: fix vm.Script createCachedData example
`Script.createCachedData` and `SourceTextModule.createCachedData` doesn't serialize JavaScript variables. PR-URL: #44487 Reviewed-By: Joyee Cheung <[email protected]>
1 parent 5c69813 commit 10891e6

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

doc/api/vm.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ Creates a code cache that can be used with the `Script` constructor's
138138
`cachedData` option. Returns a `Buffer`. This method may be called at any
139139
time and any number of times.
140140

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+
141151
```js
142152
const script = new vm.Script(`
143153
function add(a, b) {
@@ -147,11 +157,14 @@ function add(a, b) {
147157
const x = add(1, 2);
148158
`);
149159

150-
const cacheWithoutX = script.createCachedData();
160+
const cacheWithoutAdd = script.createCachedData();
161+
// In `cacheWithoutAdd` the function `add()` is marked for full compilation
162+
// upon invocation.
151163

152164
script.runInThisContext();
153165

154-
const cacheWithX = script.createCachedData();
166+
const cacheWithAdd = script.createCachedData();
167+
// `cacheWithAdd` contains fully compiled function `add()`.
155168
```
156169

157170
### `script.runInContext(contextifiedObject[, options])`
@@ -792,6 +805,16 @@ Creates a code cache that can be used with the `SourceTextModule` constructor's
792805
`cachedData` option. Returns a `Buffer`. This method may be called any number
793806
of times before the module has been evaluated.
794807

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+
795818
```js
796819
// Create an initial module
797820
const module = new vm.SourceTextModule('const a = 1;');

0 commit comments

Comments
 (0)