Skip to content

Commit cda3927

Browse files
committed
benchmark: add vm.SourceTextModule benchmark
1 parent 1241e04 commit cda3927

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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', 'instantiate', 'evaluate'],
9+
n: [1000],
10+
}, {
11+
flags: ['--experimental-vm-modules'],
12+
});
13+
14+
function main({ stage, n }) {
15+
const arr = [new vm.SourceTextModule(`
16+
export const value = 42;
17+
`)];
18+
19+
if (stage === 'all') {
20+
bench.start();
21+
}
22+
23+
for (let i = 0; i < n; i++) {
24+
const m = new vm.SourceTextModule(`
25+
export { value } from 'mod${i}';
26+
`);
27+
arr.push(m);
28+
m.linkRequests([arr[i]]);
29+
}
30+
31+
if (stage === 'instantiate') {
32+
bench.start();
33+
}
34+
arr[n].instantiate();
35+
if (stage === 'instantiate') {
36+
bench.end(n);
37+
}
38+
39+
if (stage === 'evaluate') {
40+
bench.start();
41+
}
42+
arr[n].evaluate();
43+
if (stage === 'evaluate' || stage === 'all') {
44+
bench.end(n);
45+
}
46+
47+
assert.strictEqual(arr[n].namespace.value, 42);
48+
}
49+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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', 'link', 'instantiate', 'evaluate'],
9+
n: [1000],
10+
}, {
11+
flags: ['--experimental-vm-modules'],
12+
});
13+
14+
function main({ stage, n }) {
15+
const arr = [];
16+
let importSource = '';
17+
let useSource = 'export const result = 0 ';
18+
for (let i = 0; i < n; i++) {
19+
importSource += `import { value${i} } from 'mod${i}';\n`;
20+
useSource += ` + value${i}\n`;
21+
}
22+
23+
if (stage === 'all') {
24+
bench.start();
25+
}
26+
for (let i = 0; i < n; i++) {
27+
const m = new vm.SourceTextModule(`
28+
export const value${i} = 1;
29+
`);
30+
arr.push(m);
31+
}
32+
33+
const root = new vm.SourceTextModule(`
34+
${importSource}
35+
${useSource};
36+
`);
37+
38+
if (stage === 'link') {
39+
bench.start();
40+
}
41+
42+
root.linkRequests(arr);
43+
for (let i = 0; i < n; i++) {
44+
arr[i].linkRequests([]);
45+
}
46+
47+
if (stage === 'link') {
48+
bench.end(n);
49+
}
50+
51+
if (stage === 'instantiate') {
52+
bench.start();
53+
}
54+
root.instantiate();
55+
if (stage === 'instantiate') {
56+
bench.end(n);
57+
}
58+
59+
if (stage === 'evaluate') {
60+
bench.start();
61+
}
62+
root.evaluate();
63+
if (stage === 'evaluate' || stage === 'all') {
64+
bench.end(n);
65+
}
66+
67+
assert.strictEqual(root.namespace.result, n);
68+
}
69+

0 commit comments

Comments
 (0)