@@ -159,6 +159,11 @@ namespace ts {
159
159
let timerHandleForRecompilation : number ; // Handle for 0.25s wait timer to trigger recompilation
160
160
let timerHandleForDirectoryChanges : number ; // Handle for 0.25s wait timer to trigger directory change handler
161
161
162
+ // This map stores and reuses results of fileExists check that happen inside 'createProgram'
163
+ // This allows to save time in module resolution heavy scenarios when existence of the same file might be checked multiple times.
164
+ let cachedExistingFiles : Map < boolean > ;
165
+ let hostFileExists : typeof compilerHost . fileExists ;
166
+
162
167
if ( commandLine . options . locale ) {
163
168
if ( ! isJSONSupported ( ) ) {
164
169
reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--locale" ) ) ;
@@ -274,8 +279,14 @@ namespace ts {
274
279
compilerHost = createCompilerHost ( compilerOptions ) ;
275
280
hostGetSourceFile = compilerHost . getSourceFile ;
276
281
compilerHost . getSourceFile = getSourceFile ;
282
+
283
+ hostFileExists = compilerHost . fileExists ;
284
+ compilerHost . fileExists = cachedFileExists ;
277
285
}
278
286
287
+ // reset the cache of existing files
288
+ cachedExistingFiles = { } ;
289
+
279
290
let compileResult = compile ( rootFileNames , compilerOptions , compilerHost ) ;
280
291
281
292
if ( ! compilerOptions . watch ) {
@@ -286,6 +297,13 @@ namespace ts {
286
297
reportWatchDiagnostic ( createCompilerDiagnostic ( Diagnostics . Compilation_complete_Watching_for_file_changes ) ) ;
287
298
}
288
299
300
+ function cachedFileExists ( fileName : string ) : boolean {
301
+ if ( hasProperty ( cachedExistingFiles , fileName ) ) {
302
+ return cachedExistingFiles [ fileName ] ;
303
+ }
304
+ return cachedExistingFiles [ fileName ] = hostFileExists ( fileName ) ;
305
+ }
306
+
289
307
function getSourceFile ( fileName : string , languageVersion : ScriptTarget , onError ?: ( message : string ) => void ) {
290
308
// Return existing SourceFile object if one is available
291
309
if ( cachedProgram ) {
0 commit comments