Skip to content
Permalink
Browse files

Cache file system operations (#803)

  • Loading branch information...
ksamborski authored and blakeembrey committed Apr 15, 2019
1 parent 038f83d commit 66d54af847e9c49d7e794a438f5e82c421d7b187
Showing with 14 additions and 2 deletions.
  1. +14 −2 src/index.ts
@@ -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.
*/
@@ -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,

0 comments on commit 66d54af

Please sign in to comment.
You can’t perform that action at this time.