Skip to content

Commit cc55e84

Browse files
legendecasjuanarbol
authored andcommittedOct 11, 2022
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 7b3a2c3 commit cc55e84

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
@@ -136,6 +136,16 @@ Creates a code cache that can be used with the `Script` constructor's
136136
`cachedData` option. Returns a `Buffer`. This method may be called at any
137137
time and any number of times.
138138

139+
The code cache of the `Script` doesn't contain any JavaScript observable
140+
states. The code cache is safe to be saved along side the script source and
141+
used to construct new `Script` instances multiple times.
142+
143+
Functions in the `Script` source can be marked as lazily compiled and they are
144+
not compiled at construction of the `Script`. These functions are going to be
145+
compiled when they are invoked the first time. The code cache serializes the
146+
metadata that V8 currently knows about the `Script` that it can use to speed up
147+
future compilations.
148+
139149
```js
140150
const script = new vm.Script(`
141151
function add(a, b) {
@@ -145,11 +155,14 @@ function add(a, b) {
145155
const x = add(1, 2);
146156
`);
147157

148-
const cacheWithoutX = script.createCachedData();
158+
const cacheWithoutAdd = script.createCachedData();
159+
// In `cacheWithoutAdd` the function `add()` is marked for full compilation
160+
// upon invocation.
149161

150162
script.runInThisContext();
151163

152-
const cacheWithX = script.createCachedData();
164+
const cacheWithAdd = script.createCachedData();
165+
// `cacheWithAdd` contains fully compiled function `add()`.
153166
```
154167

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

804+
The code cache of the `SourceTextModule` doesn't contain any JavaScript
805+
observable states. The code cache is safe to be saved along side the script
806+
source and used to construct new `SourceTextModule` instances multiple times.
807+
808+
Functions in the `SourceTextModule` source can be marked as lazily compiled
809+
and they are not compiled at construction of the `SourceTextModule`. These
810+
functions are going to be compiled when they are invoked the first time. The
811+
code cache serializes the metadata that V8 currently knows about the
812+
`SourceTextModule` that it can use to speed up future compilations.
813+
791814
```js
792815
// Create an initial module
793816
const module = new vm.SourceTextModule('const a = 1;');

0 commit comments

Comments
 (0)
Please sign in to comment.