Skip to content

Commit 5d75ec4

Browse files
anonrigMoLow
authored andcommitted
module: reduce the number of URL initializations
PR-URL: #48272 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1cde4a4 commit 5d75ec4

3 files changed

Lines changed: 18 additions & 15 deletions

File tree

‎lib/internal/main/check_syntax.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// If user passed `-c` or `--check` arguments to Node, check its syntax
44
// instead of actually running the file.
55

6+
const{URL}=require('internal/url');
67
const{
78
prepareMainThreadExecution,
89
markBootstrapComplete,
@@ -55,7 +56,7 @@ async function checkSyntax(source, filename) {
5556
const{ defaultResolve }=require('internal/modules/esm/resolve');
5657
const{ defaultGetFormat }=require('internal/modules/esm/get_format');
5758
const{ url }=awaitdefaultResolve(pathToFileURL(filename).toString());
58-
constformat=awaitdefaultGetFormat(url);
59+
constformat=awaitdefaultGetFormat(newURL(url));
5960
isModule=format==='module';
6061
}
6162
if(isModule){

‎lib/internal/modules/esm/get_format.js‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const experimentalNetworkImports =
2020
constexperimentalSpecifierResolution=
2121
getOptionValue('--experimental-specifier-resolution');
2222
const{ getPackageType, getPackageScopeConfig }=require('internal/modules/esm/resolve');
23-
const{URL,fileURLToPath }=require('internal/url');
23+
const{ fileURLToPath }=require('internal/url');
2424
const{ERR_UNKNOWN_FILE_EXTENSION}=require('internal/errors').codes;
2525

2626
constprotocolHandlers={
@@ -99,27 +99,29 @@ function getHttpProtocolModuleFormat(url, context) {
9999
}
100100

101101
/**
102-
* @param {URL | URL['href']} url
102+
* @param {URL} url
103103
* @param {{parentURL: string}} context
104104
* @returns {Promise<string> | string | undefined} only works when enabled
105105
*/
106106
functiondefaultGetFormatWithoutErrors(url,context){
107-
constparsed=newURL(url);
108-
if(!ObjectPrototypeHasOwnProperty(protocolHandlers,parsed.protocol))
107+
constprotocol=url.protocol;
108+
if(!ObjectPrototypeHasOwnProperty(protocolHandlers,protocol)){
109109
returnnull;
110-
returnprotocolHandlers[parsed.protocol](parsed,context,true);
110+
}
111+
returnprotocolHandlers[protocol](url,context,true);
111112
}
112113

113114
/**
114-
* @param {URL | URL['href']} url
115+
* @param {URL} url
115116
* @param {{parentURL: string}} context
116117
* @returns {Promise<string> | string | undefined} only works when enabled
117118
*/
118119
functiondefaultGetFormat(url,context){
119-
constparsed=newURL(url);
120-
returnObjectPrototypeHasOwnProperty(protocolHandlers,parsed.protocol) ?
121-
protocolHandlers[parsed.protocol](parsed,context,false) :
122-
null;
120+
constprotocol=url.protocol;
121+
if(!ObjectPrototypeHasOwnProperty(protocolHandlers,protocol)){
122+
returnnull;
123+
}
124+
returnprotocolHandlers[protocol](url,context,false);
123125
}
124126

125127
module.exports={

‎lib/internal/modules/esm/load.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ async function defaultLoad(url, context) {
7777
source,
7878
}=context;
7979

80-
throwIfUnsupportedURLScheme(newURL(url),experimentalNetworkImports);
80+
consturlInstance=newURL(url);
8181

82-
if(format==null){
83-
format=awaitdefaultGetFormat(url,context);
84-
}
82+
throwIfUnsupportedURLScheme(urlInstance,experimentalNetworkImports);
83+
84+
format??=awaitdefaultGetFormat(urlInstance,context);
8585

8686
validateAssertions(url,format,importAssertions);
8787

0 commit comments

Comments
 (0)