Skip to content

Commit

Permalink
benchmark: add a benchmark for defaultResolve
Browse files Browse the repository at this point in the history
PR-URL: #47543
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
  • Loading branch information
aduh95 authored and targos committed May 2, 2023
1 parent 9e3e0b0 commit 936d7cb
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions benchmark/esm/esm-loader-defaultResolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Tests the impact on eager operations required for policies affecting
// general startup, does not test lazy operations
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const common = require('../common.js');

const tmpdir = require('../../test/common/tmpdir.js');
const { pathToFileURL } = require('node:url');

const benchmarkDirectory =
path.resolve(tmpdir.path, 'benchmark-import-meta-resolve');

const parentURL = pathToFileURL(path.join(benchmarkDirectory, 'entry-point.js'));

const configs = {
n: [1e3],
specifier: [
'./relative-existing.js',
'./relative-nonexistent.js',
'unprefixed-existing',
'unprefixed-nonexistent',
'node:prefixed-nonexistent',
'node:os',
],
};

const options = {
flags: ['--expose-internals'],
};

const bench = common.createBenchmark(main, configs, options);

function main(conf) {
const { defaultResolve } = require('internal/modules/esm/resolve');
tmpdir.refresh();

fs.mkdirSync(path.join(benchmarkDirectory, 'node_modules', 'unprefixed-existing'), { recursive: true });
fs.writeFileSync(path.join(benchmarkDirectory, 'node_modules', 'unprefixed-existing', 'index.js'), '\n');
fs.writeFileSync(path.join(benchmarkDirectory, 'relative-existing.js'), '\n');

bench.start();

for (let i = 0; i < conf.n; i++) {
try {
defaultResolve(conf.specifier, { parentURL });
} catch { /* empty */ }
}

bench.end(conf.n);
}

0 comments on commit 936d7cb

Please sign in to comment.