Skip to content

Commit

Permalink
Cache file system operations (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksamborski authored and blakeembrey committed Apr 15, 2019
1 parent 038f83d commit 66d54af
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ export interface Register {
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
}

function cachedLookup (fn: (arg: string) => boolean): (arg: string) => boolean {
const cache = new Map<string, boolean>()

return (arg: string) => {
if (!cache.has(arg)) {
cache.set(arg, fn(arg))
}

return cache.get(arg) || false
}
}

/**
* Register TypeScript compiler.
*/
Expand Down Expand Up @@ -303,11 +315,11 @@ export function register (opts: Options = {}): Register {

return ts.ScriptSnapshot.fromString(contents)
},
fileExists: debugFn('fileExists', fileExists),
fileExists: cachedLookup(debugFn('fileExists', fileExists)),
readFile: debugFn('readFile', readFile),
readDirectory: debugFn('readDirectory', ts.sys.readDirectory),
getDirectories: debugFn('getDirectories', ts.sys.getDirectories),
directoryExists: debugFn('directoryExists', ts.sys.directoryExists),
directoryExists: cachedLookup(debugFn('directoryExists', ts.sys.directoryExists)),
realpath: debugFn('realpath', ts.sys.realpath!),
getNewLine: () => ts.sys.newLine,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
Expand Down

0 comments on commit 66d54af

Please sign in to comment.