script.createCachedData() Method in Node.js

The script.createCachedData() method in Node.js creates a code cache for VM scripts, improving performance when executing the same script multiple times. This method is part of the vm module and generates cached bytecode that can be reused.

Syntax

script.createCachedData()

Parameters

This method takes no parameters. It creates cached data from the current script instance and returns a Buffer containing the cached bytecode.

Return Value

Returns a Buffer object containing the cached bytecode that can be used with the cachedData option when creating new script instances.

Example 1: Basic Cached Data Creation

This example demonstrates creating cached data without executing the script first:

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

// Creating a script that subtracts two numbers
const script = new vm.Script(`
    function subtract(a, b) {
        return a - b;
    }
    const result = subtract(5, 3);
`);

// Creating cached data without running the script
const cacheWithoutExecution = script.createCachedData();
console.log("Cache size:", cacheWithoutExecution.length);
console.log("Cache type:", typeof cacheWithoutExecution);
console.log("Is Buffer:", Buffer.isBuffer(cacheWithoutExecution));
Cache size: 176
Cache type: object
Is Buffer: true

Example 2: Cached Data After Execution

This example shows how cached data differs when created after script execution:

const vm = require("vm");

const script = new vm.Script(`
    function add(a, b) {
        return a + b;
    }
    const x = add(10, 20);
    x; // Return the result
`);

// Execute the script first
const result = script.runInThisContext();
console.log("Script result:", result);

// Create cached data after execution
const cacheAfterExecution = script.createCachedData();
console.log("Cache size after execution:", cacheAfterExecution.length);
Script result: 30
Cache size after execution: 176

Example 3: Using Cached Data with New Script

This example demonstrates how to use the cached data with a new script instance:

const vm = require("vm");

const code = `
    function multiply(a, b) {
        return a * b;
    }
    multiply(4, 7);
`;

// Create original script and generate cache
const originalScript = new vm.Script(code);
originalScript.runInThisContext();
const cachedData = originalScript.createCachedData();

// Create new script using cached data
const newScript = new vm.Script(code, { cachedData });

console.log("Cache rejected:", newScript.cachedDataRejected);
const result = newScript.runInThisContext();
console.log("Result from cached script:", result);
Cache rejected: false
Result from cached script: 28

Key Points

  • Cached data improves performance for repeatedly executed scripts
  • The cache is specific to the V8 version and may not work across different versions
  • Use cachedDataRejected property to check if cached data was successfully used
  • Cached data is most beneficial for complex scripts with significant compilation overhead

Conclusion

The createCachedData() method is valuable for optimizing script performance in Node.js applications. It generates reusable bytecode that reduces compilation time for frequently executed scripts.

Updated on: 2026-03-15T23:19:00+05:30

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements