Skip to content

Commit fbce3b9

Browse files
aduh95danielleadams
authored andcommitted
benchmark: add a benchmark for defaultResolve
PR-URL: #47543 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent cacd424 commit fbce3b9

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Tests the impact on eager operations required for policies affecting
2+
// general startup, does not test lazy operations
3+
'use strict';
4+
const fs = require('node:fs');
5+
const path = require('node:path');
6+
const common = require('../common.js');
7+
8+
const tmpdir = require('../../test/common/tmpdir.js');
9+
const { pathToFileURL } = require('node:url');
10+
11+
const benchmarkDirectory =
12+
path.resolve(tmpdir.path, 'benchmark-import-meta-resolve');
13+
14+
const parentURL = pathToFileURL(path.join(benchmarkDirectory, 'entry-point.js'));
15+
16+
const configs = {
17+
n: [1e3],
18+
specifier: [
19+
'./relative-existing.js',
20+
'./relative-nonexistent.js',
21+
'unprefixed-existing',
22+
'unprefixed-nonexistent',
23+
'node:prefixed-nonexistent',
24+
'node:os',
25+
],
26+
};
27+
28+
const options = {
29+
flags: ['--expose-internals'],
30+
};
31+
32+
const bench = common.createBenchmark(main, configs, options);
33+
34+
function main(conf) {
35+
const { defaultResolve } = require('internal/modules/esm/resolve');
36+
tmpdir.refresh();
37+
38+
fs.mkdirSync(path.join(benchmarkDirectory, 'node_modules', 'unprefixed-existing'), { recursive: true });
39+
fs.writeFileSync(path.join(benchmarkDirectory, 'node_modules', 'unprefixed-existing', 'index.js'), '\n');
40+
fs.writeFileSync(path.join(benchmarkDirectory, 'relative-existing.js'), '\n');
41+
42+
bench.start();
43+
44+
for (let i = 0; i < conf.n; i++) {
45+
try {
46+
defaultResolve(conf.specifier, { parentURL });
47+
} catch { /* empty */ }
48+
}
49+
50+
bench.end(conf.n);
51+
}

0 commit comments

Comments
 (0)