Heim  >  Artikel  >  Web-Frontend  >  script.createCachedData()-Methode in Node.js

script.createCachedData()-Methode in Node.js

王林
王林nach vorne
2023-09-04 21:21:081148Durchsuche

Die Methode

Node.js 中的 script.createCachedData() 方法

script.createCachedData() wird verwendet, um einen Code-Cache zu erstellen, der mit der Option „cachedData“ des Skriptkonstruktors verwendet wird. Diese zwischengespeicherten Daten können ohne Verzögerung mehrmals aufgerufen werden. Diese Methode ist die integrierte Programmierschnittstelle des Skriptmoduls.

Syntax

script.createCachedData()

Parameter

weil nur Daten zwischengespeichert werden. Es sind keine spezifischen Eingaben des Benutzers erforderlich. Es wird nur der zwischengespeicherte Puffer zurückgegeben.

Beispiel

Erstellen Sie eine Datei mit dem Namen createCachedData.js und kopieren Sie den folgenden Codeausschnitt. Nachdem Sie die Datei erstellt haben, führen Sie diesen Code mit dem folgenden Befehl aus, wie im folgenden Beispiel gezeigt:

node createCachedData.js

createCachedData.js

// Node.js program to demonstrate the flow of script.createCachedData() method

// Importing the vm module
const vm = require("vm");

// Defining the script as constant and usng the below function
// to create the cached data for the difference of two numbers.
const script = new vm.Script(`
   function add(a, b) {
      return a - b;
   }
   const x = add(2, 1);
`);

//Creating the cached data without caching the variable
const cacheWithoutx = script.createCachedData();
console.log(cacheWithoutx);

Output

C:\homeode>> node createCachedData.js
<Buffer b5 03 de c0 8a f4 d4 f4 3d 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00
00 00 00 00 00 28 02 00 00 8f 87 4d e3 59 55 98 f9 00 00 00 80 20 00 00 80 00
03 ... >

Example

Sehen wir uns noch ein Beispiel an.

// Node.js program to demonstrate the flow of script.createCachedData() method

// Importing the vm module
const vm = require("vm");

// Defining the script as constant and usng the below function
// to create the cached data for the difference of two numbers.
const script = new vm.Script(`
   function add(a, b) {
      return a - b;
   }
   const x = add(2, 1);
`);

// Calling the runInThisContext from script module
script.runInThisContext();

//Creating the cached data along with caching the variable
const cacheWithx = script.createCachedData();
console.log(cacheWithx);

Ausgabe

C:\homeode>> node createCachedData.js
<Buffer b5 03 de c0 8a f4 d4 f4 3d 00 00 00 ff 03 00 00 d5 a2 f5 b7 06 00 00
00 00 00 00 00 00 03 00 00 15 80 fd 5d 69 21 3a a9 00 00 00 80 20 00 00 80 38
04 ... >

Das obige ist der detaillierte Inhalt vonscript.createCachedData()-Methode in Node.js. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen