Skip to content

Commit 5b8bcb0

Browse files
committed
Improve fs performance by removing extra exists/stat checks
Hopefully fixes #1819.
1 parent 9b7faf7 commit 5b8bcb0

4 files changed

Lines changed: 18 additions & 17 deletions

File tree

‎src/extension/commands/open_in_other_editors.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ export class OpenInOtherEditorCommands implements vs.Disposable {
3737
private async openInXcode(resource: vs.Uri): Promise<void> {
3838
const folder = fsPath(resource);
3939
const files = fs
40-
.readdirSync(folder)
41-
.filter((item) => fs.statSync(path.join(folder, item)).isDirectory())
42-
.filter((item) => item.endsWith(".xcworkspace") || item.endsWith(".xcodeproj"))
43-
.sort((f1, f2) => f1.endsWith(".xcworkspace") ? 0 : 1);
40+
.readdirSync(folder, { withFileTypes: true })
41+
.filter((item) => item.isDirectory())
42+
.filter((item) => item.name.endsWith(".xcworkspace") || item.name.endsWith(".xcodeproj"))
43+
.sort((f1, f2) => f1.name.endsWith(".xcworkspace") ? 0 : 1);
4444

4545
if (!files || !files.length) {
4646
vs.window.showErrorMessage(`Unable to find an Xcode project in your 'ios' folder`);
4747
return;
4848
}
4949

50-
const file = path.join(folder, files[0]);
50+
const file = path.join(folder, files[0].name);
5151
safeSpawn(folder, "open", ["-a", "Xcode", file]);
5252
}
5353

‎src/extension/sdk/sdk_manager.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ abstract class SdkManager {
3939
allPaths.push(this.currentSdk);
4040

4141
const sdkFolders = allPaths
42-
.filter((f) => fs.statSync(f).isDirectory()) // Only directories.
42+
// We don't need to check isDirectory, since existsSync() will just return false
43+
// if we join a filename on the end of another.
44+
// .filter((f) => fs.statSync(f).isDirectory()) // Only directories.
4345
.filter((f) => fs.existsSync(path.join(f, this.executablePath))); // Only those that look like SDKs.
4446

4547
const sdkItems: SdkPickItem[] = sdkFolders.map((f) => {

‎src/extension/views/packages_view.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,15 @@ export class DartPackagesProvider implements vs.Disposable, vs.TreeDataProvider<
6565
}
6666

6767
private getFilesAndFolders(folder: PackageDepFolder): PackageDep[] {
68-
const childNames = sortBy(fs.readdirSync(fsPath(folder.resourceUri)), (s) => s.toLowerCase());
68+
const children = sortBy(fs.readdirSync(fsPath(folder.resourceUri), { withFileTypes: true }), (s) => s.name.toLowerCase());
6969
const folders: PackageDepFolder[] = [];
7070
const files: PackageDepFile[] = [];
7171

72-
childNames.forEach((name) => {
73-
const filePath = path.join(fsPath(folder.resourceUri), name);
74-
const stat = fs.statSync(filePath);
75-
if (stat.isFile()) {
72+
children.forEach((child) => {
73+
const filePath = path.join(fsPath(folder.resourceUri), child.name);
74+
if (child.isFile()) {
7675
files.push(new PackageDepFile(vs.Uri.file(filePath)));
77-
} else if (stat.isDirectory()) {
76+
} else if (child.isDirectory()) {
7877
folders.push(new PackageDepFolder(vs.Uri.file(filePath)));
7978
}
8079
});

‎src/shared/utils/fs.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { sortBy } from "./array";
77
export function getChildFolders(parent: string, options?: { allowBin?: boolean, allowCache?: boolean }): string[] {
88
if (!fs.existsSync(parent))
99
return [];
10-
return fs.readdirSync(parent)
11-
.filter((f) => f !== "bin" || (options && options.allowBin)) // Don't look in bin folders
12-
.filter((f) => f !== "cache" || (options && options.allowCache)) // Don't look in cache folders
13-
.map((item) => path.join(parent, item))
14-
.filter((item) => fs.existsSync(item) && fs.statSync(item).isDirectory());
10+
return fs.readdirSync(parent, { withFileTypes: true })
11+
.filter((f) => f.isDirectory())
12+
.filter((f) => f.name !== "bin" || (options && options.allowBin)) // Don't look in bin folders
13+
.filter((f) => f.name !== "cache" || (options && options.allowCache)) // Don't look in cache folders
14+
.map((item) => path.join(parent, item.name));
1515
}
1616

1717
export function hasPackagesFile(folder: string): boolean {

0 commit comments

Comments
 (0)