The best way to control Claude Code, Codex and more from any computer or phone
perf(web): reuse a collator when sorting changed-file trees (#23776)
Utkarsh Patil committed
Oct 23, 2026 at 19:11 UTC
9828cad49f3e1ef4f9e037b3baaca19f4e4928fc
2 files changed
+42
-2
apps/web/src/lib/turnDiffTree.test.ts
+38
@@ -3,6 +3,44 @@ import { describe, expect, it } from "vite-plus/test";
3
import { buildTurnDiffTree } from "./turnDiffTree";
4
5
describe("buildTurnDiffTree", () => {
6
+ it("sorts directory and file names numerically with directories first", () => {
7
+ const tree = buildTurnDiffTree([
8
+ { path: "file10.ts", kind: "modified", additions: 1, deletions: 0 },
9
+ { path: "dir10/file10.ts", kind: "modified", additions: 1, deletions: 0 },
10
+ { path: "file2.ts", kind: "modified", additions: 1, deletions: 0 },
11
+ { path: "dir2/file10.ts", kind: "modified", additions: 1, deletions: 0 },
12
+ { path: "dir2/file2.ts", kind: "modified", additions: 1, deletions: 0 },
13
+ ]);
14
+
15
+ expect(tree.map((node) => node.name)).toEqual(["dir2", "dir10", "file2.ts", "file10.ts"]);
16
+ const directory = tree[0];
17
+ expect(directory?.kind).toBe("directory");
18
+ if (directory?.kind !== "directory") throw new Error("Expected a directory");
19
+ expect(directory.children.map((node) => node.name)).toEqual(["file2.ts", "file10.ts"]);
20
+ });
21
+
22
+ it("preserves input order for case- and accent-insensitive name ties", () => {
23
+ const tree = buildTurnDiffTree([
24
+ { path: "a.ts", kind: "modified", additions: 1, deletions: 0 },
25
+ { path: "A.ts", kind: "modified", additions: 2, deletions: 0 },
26
+ { path: "FOLDER/a.ts", kind: "modified", additions: 3, deletions: 0 },
27
+ { path: "folder/a.ts", kind: "modified", additions: 4, deletions: 0 },
28
+ { path: "café.ts", kind: "modified", additions: 5, deletions: 0 },
29
+ { path: "Cafe.ts", kind: "modified", additions: 6, deletions: 0 },
30
+ { path: "cafe.ts", kind: "modified", additions: 7, deletions: 0 },
31
+ ]);
32
+
33
+ expect(tree.map((node) => node.name)).toEqual([
34
+ "FOLDER",
35
+ "folder",
36
+ "a.ts",
37
+ "A.ts",
38
+ "café.ts",
39
+ "Cafe.ts",
40
+ "cafe.ts",
41
+ ]);
42
+ });
43
+
44
it("builds nested directory nodes with aggregated stats", () => {
45
const tree = buildTurnDiffTree([
46
{ path: "src/index.ts", kind: "modified", additions: 2, deletions: 1 },
apps/web/src/lib/turnDiffTree.ts
+4
-2
@@ -30,7 +30,7 @@ interface MutableDirectoryNode {
30
files: TurnDiffTreeFileNode[];
31
}
32
33
-const SORT_LOCALE_OPTIONS: Intl.CollatorOptions = { numeric: true, sensitivity: "base" };
33
+let sortCollator: Intl.Collator | undefined;
34
35
function normalizePathSegments(pathValue: string): string[] {
36
return pathValue
@@ -40,7 +40,9 @@ function normalizePathSegments(pathValue: string): string[] {
40
}
41
42
function compareByName(a: { name: string }, b: { name: string }): number {
43
- return a.name.localeCompare(b.name, undefined, SORT_LOCALE_OPTIONS);
43
+ // Defer ICU initialization until the first name comparison.
44
+ sortCollator ??= new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
45
+ return sortCollator.compare(a.name, b.name);
46
}
47
48
function readStat(file: TurnDiffFileChange): TurnDiffStat | null {