forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: detect ESM syntax in ambiguous JavaScript
PR-URL: nodejs#50096 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
- Loading branch information
1 parent
c518cd0
commit 87bf5f8
Showing
30 changed files
with
475 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
// This benchmarks the cost of running `containsModuleSyntax` on a CommonJS module being imported. | ||
// 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'], | ||
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(benchmarkDirectory, { recursive: true }); | ||
fs.cpSync(scriptPath, ambiguousURL); | ||
fs.cpSync(scriptPath, explicitURL); | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; i++) { | ||
const url = type === 'with-module-syntax-detection' ? ambiguousURL : explicitURL; | ||
await import(url); | ||
} | ||
|
||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.