Skip to content

Commit 4eeeab0

Browse files
authored
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 54b5ec9 commit 4eeeab0

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

‎benchmark/esm/detect-esm-syntax.js‎

Lines changed: 16 additions & 17 deletions
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
constcommon=require('../common.js');
66
consttmpdir=require('../../test/common/tmpdir.js');
7-
constfixtures=require('../../test/common/fixtures.js');
8-
constscriptPath=fixtures.path('snapshot','typescript.js');
97
constfs=require('node:fs');
108

119
constbench=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-
constbenchmarkDirectory=tmpdir.fileURL('benchmark-detect-esm-syntax');
19-
constambiguousURL=newURL('./typescript.js',benchmarkDirectory);
20-
constexplicitURL=newURL('./typescript.cjs',benchmarkDirectory);
21-
2214
asyncfunctionmain({ 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+
letloader='';
19+
constmodules=[];
3120
for(leti=0;i<n;i++){
32-
consturl=type==='with-module-syntax-detection' ? ambiguousURL : explicitURL;
33-
awaitimport(url);
21+
consturl=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+
constloaderURL=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+
awaitimport(loaderURL);
3635
bench.end(n);
3736
}

0 commit comments

Comments
(0)