@@ -159,6 +159,11 @@ namespace ts {
159159 let timerHandleForRecompilation : number ; // Handle for 0.25s wait timer to trigger recompilation
160160 let timerHandleForDirectoryChanges : number ; // Handle for 0.25s wait timer to trigger directory change handler
161161
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+
162167 if ( commandLine . options . locale ) {
163168 if ( ! isJSONSupported ( ) ) {
164169 reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--locale" ) ) ;
@@ -274,8 +279,14 @@ namespace ts {
274279 compilerHost = createCompilerHost ( compilerOptions ) ;
275280 hostGetSourceFile = compilerHost . getSourceFile ;
276281 compilerHost . getSourceFile = getSourceFile ;
282+
283+ hostFileExists = compilerHost . fileExists ;
284+ compilerHost . fileExists = cachedFileExists ;
277285 }
278286
287+ // reset the cache of existing files
288+ cachedExistingFiles = { } ;
289+
279290 let compileResult = compile ( rootFileNames , compilerOptions , compilerHost ) ;
280291
281292 if ( ! compilerOptions . watch ) {
@@ -286,6 +297,13 @@ namespace ts {
286297 reportWatchDiagnostic ( createCompilerDiagnostic ( Diagnostics . Compilation_complete_Watching_for_file_changes ) ) ;
287298 }
288299
300+ function cachedFileExists ( fileName : string ) : boolean {
301+ if ( hasProperty ( cachedExistingFiles , fileName ) ) {
302+ return cachedExistingFiles [ fileName ] ;
303+ }
304+ return cachedExistingFiles [ fileName ] = hostFileExists ( fileName ) ;
305+ }
306+
289307 function getSourceFile ( fileName : string , languageVersion : ScriptTarget , onError ?: ( message : string ) => void ) {
290308 // Return existing SourceFile object if one is available
291309 if ( cachedProgram ) {
0 commit comments