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);
ログイン後にコピー

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

Let's 別の例を見てみましょう。

// 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 中国語 Web サイトの他の関連記事を参照してください。

ソース:tutorialspoint.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!