Skip to content

Commit a12fe2e

Browse files
author
Arthur Ozga
committed
Eliminate allocation of filtered completions
1 parent 1baf496 commit a12fe2e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/services/completions.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ namespace ts.Completions {
351351
const files = tryReadDirectory(host, baseDirectory, extensions, /*exclude*/undefined, /*include*/["./*"]);
352352

353353
if (files) {
354+
/**
355+
* Multiple file entries might map to the same truncated name once we remove extensions
356+
* (happens iff includeExtensions === false)so we use a set-like data structure. Eg:
357+
*
358+
* both foo.ts and foo.tsx become foo
359+
*/
354360
const foundFiles = createMap<boolean>();
355361
for (let filePath of files) {
356362
filePath = normalizePath(filePath);
@@ -360,6 +366,11 @@ namespace ts.Completions {
360366

361367
const foundFileName = includeExtensions ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
362368

369+
// Only add entries that contain the basename as a substring.
370+
if (foundFileName.indexOf(baseName) === -1) {
371+
continue;
372+
}
373+
363374
if (!foundFiles[foundFileName]) {
364375
foundFiles[foundFileName] = true;
365376
}
@@ -377,13 +388,16 @@ namespace ts.Completions {
377388
for (const directory of directories) {
378389
const directoryName = getBaseFileName(normalizePath(directory));
379390

391+
// Only add entries that contain the basename as a substring.
392+
if (directoryName.indexOf(baseName) === -1) {
393+
continue;
394+
}
395+
380396
result.push(createCompletionEntryForModule(directoryName, ScriptElementKind.directory, span));
381397
}
382398
}
383399
}
384400

385-
result = result.filter(entry => entry.name.indexOf(baseName) >= 0);
386-
387401
return result;
388402
}
389403

0 commit comments

Comments
 (0)