Skip to content

Commit

Permalink
fix: performance issue with o(n^2) complexity of directoryExists() (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Oct 6, 2023
1 parent ab8c913 commit 368446f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/typescript/src/languageServiceHost.ts
Expand Up @@ -18,6 +18,7 @@ export function createLanguageServiceHost(
let lastProjectVersion: number | string | undefined;
let tsProjectVersion = 0;
let tsFileNames: string[] = [];
let tsDirectories: Record<string, true> = {};

const _tsHost: ts.LanguageServiceHost = {
...sys,
Expand Down Expand Up @@ -204,6 +205,12 @@ export function createLanguageServiceHost(
}
}
tsFileNames = [...tsFileNamesSet];

// Update tsDirectories for `directoryExists()`
tsDirectories = {};
for (const fileName of tsFileNames) {
tsDirectories[path.dirname(fileName)] = true;
}
}

function readDirectory(
Expand Down Expand Up @@ -323,8 +330,7 @@ export function createLanguageServiceHost(
}

function directoryExists(dirName: string): boolean {
return sys.directoryExists(dirName)
|| tsFileNames.some(fileName => fileName.toLowerCase().startsWith(dirName.toLowerCase()));
return tsDirectories[dirName] || sys.directoryExists(dirName);
}

function fileExists(fileName: string) {
Expand Down

0 comments on commit 368446f

Please sign in to comment.