Skip to content

Commit 8b52c89

Browse files
joyeecheungnodejs-github-bot
authored andcommitted
benchmark: add benchmark for leaf source text modules
PR-URL: #60205 Refs: #59656 Refs: #37648 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent d695004 commit 8b52c89

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use strict';
2+
3+
const vm = require('vm');
4+
const common = require('../common.js');
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
stage: ['all', 'compile', 'link', 'instantiate', 'evaluate'],
9+
type: ['sync', 'async'],
10+
n: [1000],
11+
}, {
12+
flags: ['--experimental-vm-modules'],
13+
});
14+
15+
function main({ stage, type, n }) {
16+
const arr = [];
17+
if (stage === 'all' || stage === 'compile') {
18+
bench.start();
19+
}
20+
21+
for (let i = 0; i < n; i++) {
22+
let source = `export const value${i} = 1;`;
23+
if (type === 'async') {
24+
source += `\nawait Promise.resolve();\n`;
25+
}
26+
const m = new vm.SourceTextModule(source);
27+
arr.push(m);
28+
}
29+
30+
if (stage === 'compile') {
31+
bench.end(n);
32+
return;
33+
}
34+
35+
if (stage === 'link') {
36+
bench.start();
37+
}
38+
39+
for (let i = 0; i < n; i++) {
40+
arr[i].linkRequests([]);
41+
}
42+
43+
if (stage === 'link') {
44+
bench.end(n);
45+
return;
46+
}
47+
48+
if (stage === 'instantiate') {
49+
bench.start();
50+
}
51+
52+
for (let i = 0; i < n; i++) {
53+
arr[i].instantiate();
54+
}
55+
56+
if (stage === 'instantiate') {
57+
bench.end(n);
58+
return;
59+
}
60+
61+
if (stage === 'evaluate') {
62+
bench.start();
63+
}
64+
65+
function finalize() {
66+
bench.end(n);
67+
for (let i = 0; i < n; i++) {
68+
assert.strictEqual(arr[i].namespace[`value${i}`], 1);
69+
}
70+
}
71+
72+
if (type === 'sync') {
73+
for (let i = 0; i < n; i++) {
74+
arr[i].evaluate();
75+
}
76+
finalize();
77+
} else {
78+
const promises = [];
79+
for (let i = 0; i < n; i++) {
80+
promises.push(arr[i].evaluate());
81+
}
82+
Promise.all(promises).then(finalize);
83+
}
84+
}

0 commit comments

Comments
 (0)