Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: ivm draft #4069

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions playground/ivm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// bench.js
import ivm from 'isolated-vm';
import { performance } from 'perf_hooks';

// This is the same transformation code used in index.js.
// It registers a global `transform` function that takes an object,
// adds an "id" attribute with a random UUID, and returns the object.
const transformationCode = `
global.transform = function(input) {
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = (c === 'x') ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
input.id = generateUUID();
return input;
};
`;

async function setup() {
// Create an isolate with an 8 MB memory limit
const isolate = new ivm.Isolate({ memoryLimit: 8 });
const context = isolate.createContextSync();
const jail = context.global;
// Make the global object available in the isolate (derefInto() helps produce a plain object)
await jail.set("global", jail.derefInto());
// Compile and run the transformation code so that `transform` is registered
const script = isolate.compileScriptSync(transformationCode);
script.runSync(context);
// Retrieve the 'transform' function as a reference
const transformFn = context.global.getSync("transform", { reference: true });
return { isolate, transformFn };
}

async function benchmark(iterations) {
const { isolate, transformFn } = await setup();
const input = { name: "john" };

let transformedOutput;
// Warm-up phase (and capturing transformation result)
for (let i = 0; i < 100; i++) {
const externalInput = new ivm.ExternalCopy(input).copyInto();
transformedOutput = transformFn.applySync(undefined, [externalInput], { result: { copy: true } });
}

console.log("Transformed sample:", JSON.stringify(transformedOutput, null, 2));

// Benchmark loop
const start = performance.now();
for (let i = 0; i < iterations; i++) {
const externalInput = new ivm.ExternalCopy(input).copyInto();
// Capture transformation result on each iteration if needed:
transformedOutput = transformFn.applySync(undefined, [externalInput], { result: { copy: true } });
}
const end = performance.now();
const totalTime = end - start;
console.log(`Total time for ${iterations} iterations: ${totalTime.toFixed(2)} ms`);
console.log(`Average time per transformation: ${(totalTime / iterations).toFixed(4)} ms`);

// Clean up
isolate.dispose();
}

// Change the number of iterations as needed.
benchmark(10000).catch(console.error);
Loading
Loading