Skip to content

Commit 263526d

Browse files
joyeecheungaduh95
authored andcommitted
benchmark: rewrite detect-esm-syntax benchmark
Syntax detection has been unflagged so it's no longer meaningful to toggle the detection based on CLI flags. It was also previously benchmarking cached module imports which isn't very meaningful for subsequent loads. This patch updates the benchmark to toggle the detection based on the presence of type field in the package.json, and generates fixtures to benchmark fresh module loads. PR-URL: #55238 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent b03272b commit 263526d

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

benchmark/esm/detect-esm-syntax.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,33 @@
44
// We use the TypeScript fixture because it's a very large CommonJS file with no ESM syntax: the worst case.
55
const common = require('../common.js');
66
const tmpdir = require('../../test/common/tmpdir.js');
7-
const fixtures = require('../../test/common/fixtures.js');
8-
const scriptPath = fixtures.path('snapshot', 'typescript.js');
97
const fs = require('node:fs');
108

119
const bench = common.createBenchmark(main, {
12-
type: ['with-module-syntax-detection', 'without-module-syntax-detection'],
10+
type: ['with-package-json', 'without-package-json'],
1311
n: [1e4],
14-
}, {
15-
flags: ['--experimental-detect-module'],
1612
});
1713

18-
const benchmarkDirectory = tmpdir.fileURL('benchmark-detect-esm-syntax');
19-
const ambiguousURL = new URL('./typescript.js', benchmarkDirectory);
20-
const explicitURL = new URL('./typescript.cjs', benchmarkDirectory);
21-
2214
async function main({ n, type }) {
2315
tmpdir.refresh();
16+
fs.mkdirSync(tmpdir.resolve('bench'));
2417

25-
fs.mkdirSync(benchmarkDirectory, { recursive: true });
26-
fs.cpSync(scriptPath, ambiguousURL);
27-
fs.cpSync(scriptPath, explicitURL);
28-
29-
bench.start();
30-
18+
let loader = '';
19+
const modules = [];
3120
for (let i = 0; i < n; i++) {
32-
const url = type === 'with-module-syntax-detection' ? ambiguousURL : explicitURL;
33-
await import(url);
21+
const url = tmpdir.fileURL('bench', `mod${i}.js`);
22+
fs.writeFileSync(url, `const foo${i} = ${i};\nexport { foo${i} };\n`);
23+
loader += `import { foo${i} } from './mod${i}.js';\n`;
24+
modules.push(url);
3425
}
26+
const loaderURL = tmpdir.fileURL('bench', 'load.js');
27+
fs.writeFileSync(loaderURL, loader);
3528

29+
if (type === 'with-package-json') {
30+
fs.writeFileSync(tmpdir.resolve('bench', 'package.json'), '{ "type": "module" }');
31+
}
32+
33+
bench.start();
34+
await import(loaderURL);
3635
bench.end(n);
3736
}

0 commit comments

Comments
 (0)