Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cc55e84

Browse filesBrowse files
legendecasjuanarbol
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 <joyeec9h3@gmail.com>
1 parent 7b3a2c3 commit cc55e84
Copy full SHA for cc55e84

File tree

Expand file treeCollapse file tree

1 file changed

+25
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+25
-2
lines changed
Open diff view settings
Collapse file

‎doc/api/vm.md‎

Copy file name to clipboardExpand all lines: doc/api/vm.md
+25-2Lines changed: 25 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.