Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions benchmark/esm/detect-esm-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@
// We use the TypeScript fixture because it's a very large CommonJS file with no ESM syntax: the worst case.
const common = require('../common.js');
const tmpdir = require('../../test/common/tmpdir.js');
const fixtures = require('../../test/common/fixtures.js');
const scriptPath = fixtures.path('snapshot', 'typescript.js');
const fs = require('node:fs');

const bench = common.createBenchmark(main, {
type: ['with-module-syntax-detection', 'without-module-syntax-detection'],
type: ['with-package-json', 'without-package-json'],
n: [1e4],
}, {
flags: ['--experimental-detect-module'],
});

const benchmarkDirectory = tmpdir.fileURL('benchmark-detect-esm-syntax');
const ambiguousURL = new URL('./typescript.js', benchmarkDirectory);
const explicitURL = new URL('./typescript.cjs', benchmarkDirectory);

async function main({ n, type }) {
tmpdir.refresh();
fs.mkdirSync(tmpdir.resolve('bench'));

fs.mkdirSync(benchmarkDirectory, { recursive: true });
fs.cpSync(scriptPath, ambiguousURL);
fs.cpSync(scriptPath, explicitURL);

bench.start();

let loader = '';
const modules = [];
for (let i = 0; i < n; i++) {
const url = type === 'with-module-syntax-detection' ? ambiguousURL : explicitURL;
await import(url);
const url = tmpdir.fileURL('bench', `mod${i}.js`);
fs.writeFileSync(url, `const foo${i} = ${i};\nexport { foo${i} };\n`);
loader += `import { foo${i} } from './mod${i}.js';\n`;
modules.push(url);
}
const loaderURL = tmpdir.fileURL('bench', 'load.js');
fs.writeFileSync(loaderURL, loader);

if (type === 'with-package-json') {
fs.writeFileSync(tmpdir.resolve('bench', 'package.json'), '{ "type": "module" }');
}

bench.start();
await import(loaderURL);
bench.end(n);
}
Loading