Skip to content

Commit c47b64a

Browse files
committed
doc: fix vm.Script createCachedData example
`Script.createCachedData` and `SourceTextModule.createCachedData` doesn't serialize JavaScript variables.
1 parent d0f73d3 commit c47b64a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

doc/api/vm.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
127127
time 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
130140
const script = new vm.Script(`
131141
function add(a, b) {
@@ -135,11 +145,14 @@ function add(a, b) {
135145
const 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

140152
script.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,16 @@ 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
781794
of 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
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+
783806
```js
784807
// Create an initial module
785808
const module = new vm.SourceTextModule('const a = 1;');

0 commit comments

Comments
 (0)