|
4 | 4 | // We use the TypeScript fixture because it's a very large CommonJS file with no ESM syntax: the worst case.
|
5 | 5 | const common = require('../common.js');
|
6 | 6 | const tmpdir = require('../../test/common/tmpdir.js');
|
7 |
| -const fixtures = require('../../test/common/fixtures.js'); |
8 |
| -const scriptPath = fixtures.path('snapshot', 'typescript.js'); |
9 | 7 | const fs = require('node:fs');
|
10 | 8 |
|
11 | 9 | const bench = common.createBenchmark(main, {
|
12 |
| - type: ['with-module-syntax-detection', 'without-module-syntax-detection'], |
| 10 | + type: ['with-package-json', 'without-package-json'], |
13 | 11 | n: [1e4],
|
14 |
| -}, { |
15 |
| - flags: ['--experimental-detect-module'], |
16 | 12 | });
|
17 | 13 |
|
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 |
| - |
22 | 14 | async function main({ n, type }) {
|
23 | 15 | tmpdir.refresh();
|
| 16 | + fs.mkdirSync(tmpdir.resolve('bench')); |
24 | 17 |
|
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 = []; |
31 | 20 | 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); |
34 | 25 | }
|
| 26 | + const loaderURL = tmpdir.fileURL('bench', 'load.js'); |
| 27 | + fs.writeFileSync(loaderURL, loader); |
35 | 28 |
|
| 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); |
36 | 35 | bench.end(n);
|
37 | 36 | }
|
0 commit comments