Skip to content
TypeScript

perf(web): reuse a collator when sorting changed-file trees (#23776)

Utkarsh Patil9828cad49f3e
diff --git a/apps/web/src/lib/turnDiffTree.test.ts b/apps/web/src/lib/turnDiffTree.test.ts
index e5a57693280..582dba9569a 100644
--- a/apps/web/src/lib/turnDiffTree.test.ts
+++ b/apps/web/src/lib/turnDiffTree.test.ts
@@ -3,6 +3,44 @@ import { describe, expect, it } from "vite-plus/test";
import { buildTurnDiffTree } from "./turnDiffTree";
describe("buildTurnDiffTree", () => {
+ it("sorts directory and file names numerically with directories first", () => {
+ const tree = buildTurnDiffTree([
+ { path: "file10.ts", kind: "modified", additions: 1, deletions: 0 },
+ { path: "dir10/file10.ts", kind: "modified", additions: 1, deletions: 0 },
+ { path: "file2.ts", kind: "modified", additions: 1, deletions: 0 },
+ { path: "dir2/file10.ts", kind: "modified", additions: 1, deletions: 0 },
+ { path: "dir2/file2.ts", kind: "modified", additions: 1, deletions: 0 },
+ ]);
+
+ expect(tree.map((node) => node.name)).toEqual(["dir2", "dir10", "file2.ts", "file10.ts"]);
+ const directory = tree[0];
+ expect(directory?.kind).toBe("directory");
+ if (directory?.kind !== "directory") throw new Error("Expected a directory");
+ expect(directory.children.map((node) => node.name)).toEqual(["file2.ts", "file10.ts"]);
+ });
+
+ it("preserves input order for case- and accent-insensitive name ties", () => {
+ const tree = buildTurnDiffTree([
+ { path: "a.ts", kind: "modified", additions: 1, deletions: 0 },
+ { path: "A.ts", kind: "modified", additions: 2, deletions: 0 },
+ { path: "FOLDER/a.ts", kind: "modified", additions: 3, deletions: 0 },
+ { path: "folder/a.ts", kind: "modified", additions: 4, deletions: 0 },
+ { path: "café.ts", kind: "modified", additions: 5, deletions: 0 },
+ { path: "Cafe.ts", kind: "modified", additions: 6, deletions: 0 },
+ { path: "cafe.ts", kind: "modified", additions: 7, deletions: 0 },
+ ]);
+
+ expect(tree.map((node) => node.name)).toEqual([
+ "FOLDER",
+ "folder",
+ "a.ts",
+ "A.ts",
+ "café.ts",
+ "Cafe.ts",
+ "cafe.ts",
+ ]);
+ });
+
it("builds nested directory nodes with aggregated stats", () => {
const tree = buildTurnDiffTree([
{ path: "src/index.ts", kind: "modified", additions: 2, deletions: 1 },
diff --git a/apps/web/src/lib/turnDiffTree.ts b/apps/web/src/lib/turnDiffTree.ts
index cd9bfc831fb..810a04755e8 100644
--- a/apps/web/src/lib/turnDiffTree.ts
+++ b/apps/web/src/lib/turnDiffTree.ts
@@ -30,7 +30,7 @@ interface MutableDirectoryNode {
files: TurnDiffTreeFileNode[];
}
-const SORT_LOCALE_OPTIONS: Intl.CollatorOptions = { numeric: true, sensitivity: "base" };
+let sortCollator: Intl.Collator | undefined;
function normalizePathSegments(pathValue: string): string[] {
return pathValue
@@ -40,7 +40,9 @@ function normalizePathSegments(pathValue: string): string[] {
}
function compareByName(a: { name: string }, b: { name: string }): number {
- return a.name.localeCompare(b.name, undefined, SORT_LOCALE_OPTIONS);
+ // Defer ICU initialization until the first name comparison.
+ sortCollator ??= new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
+ return sortCollator.compare(a.name, b.name);
}
function readStat(file: TurnDiffFileChange): TurnDiffStat | null {