From 936d7cb06961ef335d53901b424237da5742381a Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 13 Apr 2023 21:49:54 +0200 Subject: [PATCH] benchmark: add a benchmark for `defaultResolve` PR-URL: https://github.com/nodejs/node/pull/47543 Reviewed-By: Yagiz Nizipli Reviewed-By: Geoffrey Booth Reviewed-By: Jacob Smith --- benchmark/esm/esm-loader-defaultResolve.js | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 benchmark/esm/esm-loader-defaultResolve.js diff --git a/benchmark/esm/esm-loader-defaultResolve.js b/benchmark/esm/esm-loader-defaultResolve.js new file mode 100644 index 00000000000000..85409a122637d5 --- /dev/null +++ b/benchmark/esm/esm-loader-defaultResolve.js @@ -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); +}