Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

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:\home\node>> 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:\home\node>> 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 ... >