1// Tests the impact on eager operations required for policies affecting
2// general startup, does not test lazy operations
3'use strict';
4const fs = require('node:fs');
5const path = require('node:path');
6const common = require('../common.js');
7
8const tmpdir = require('../../test/common/tmpdir.js');
9const { pathToFileURL } = require('node:url');
10
11const benchmarkDirectory =
12  path.resolve(tmpdir.path, 'benchmark-import-meta-resolve');
13
14const parentURL = pathToFileURL(path.join(benchmarkDirectory, 'entry-point.js'));
15
16const 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
28const options = {
29  flags: ['--expose-internals'],
30};
31
32const bench = common.createBenchmark(main, configs, options);
33
34function 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}
52