Skip to content
Closed
Changes from 1 commit
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
Next Next commit
benchmark: add benchmark for leaf source text modules
  • Loading branch information
joyeecheung committed Oct 12, 2025
commit 5237650ecc7666394a2805ca364552fdeb8840ed
84 changes: 84 additions & 0 deletions benchmark/vm/source-text-module-leaf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

const vm = require('vm');
const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
stage: ['all', 'compile', 'link', 'instantiate', 'evaluate'],
type: ['sync', 'async'],
n: [1000],
}, {
flags: ['--experimental-vm-modules'],
});

function main({ stage, type, n }) {
const arr = [];
if (stage === 'all' || stage === 'compile') {
bench.start();
}

for (let i = 0; i < n; i++) {
let source = `export const value${i} = 1;`;
if (type === 'async') {
source += `\nawait Promise.resolve();\n`;
}
const m = new vm.SourceTextModule(source);
arr.push(m);
}

if (stage === 'compile') {
bench.end(n);
return;
}

if (stage === 'link') {
bench.start();
}

for (let i = 0; i < n; i++) {
arr[i].linkRequests([]);
}

if (stage === 'link') {
bench.end(n);
return;
}

if (stage === 'instantiate') {
bench.start();
}

for (let i = 0; i < n; i++) {
arr[i].instantiate();
}

if (stage === 'instantiate') {
bench.end(n);
return;
}

if (stage === 'evaluate') {
bench.start();
}

function finalize() {
bench.end(n);
for (let i = 0; i < n; i++) {
assert.strictEqual(arr[i].namespace[`value${i}`], 1);
}
}

if (type === 'sync') {
for (let i = 0; i < n; i++) {
arr[i].evaluate();
}
finalize();
} else {
const promises = [];
for (let i = 0; i < n; i++) {
promises.push(arr[i].evaluate());
}
Promise.all(promises).then(finalize);
}
}