首頁 > web前端 > js教程 > 主體

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

王林
發布: 2023-09-04 21:21:08
轉載
1178 人瀏覽過

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

script.createCachedData() 方法用於建立程式碼緩存,該快取將與腳本建構函數的 cachedData 選項一起使用。這個cachedData可以被多次調用,沒有延遲。此方法是“腳本”模組的內建程式介面。

語法

script.createCachedData()
登入後複製

參數

因為它只快取資料。它不需要用戶的任何特定輸入。它僅傳回快取的緩衝區。

範例

建立一個名為 createCachedData.js 的檔案並複製以下程式碼片段。建立檔案後,使用以下指令執行此程式碼,如下例所示-

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);
登入後複製

輸出

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 ... >
登入後複製

範例

#讓我們再看一個範例。

// 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);
登入後複製

輸出

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 ... >
登入後複製

以上是Node.js 中的 script.createCachedData() 方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!