Skip to content

Commit 3a23614

Browse files
perf(web): reuse a collator when sorting changed-file trees
1 parent 4eae8ae commit 3a23614

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎apps/web/src/lib/turnDiffTree.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ import { describe, expect, it } from "vite-plus/test";
33
import { buildTurnDiffTree } from "./turnDiffTree";
44

55
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+
644
it("builds nested directory nodes with aggregated stats", () => {
745
const tree = buildTurnDiffTree([
846
{ path: "src/index.ts", kind: "modified", additions: 2, deletions: 1 },

‎apps/web/src/lib/turnDiffTree.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface MutableDirectoryNode {
3030
files: TurnDiffTreeFileNode[];
3131
}
3232

33-
const SORT_LOCALE_OPTIONS: Intl.CollatorOptions = { numeric: true, sensitivity: "base" };
33+
let sortCollator: Intl.Collator | undefined;
3434

3535
function normalizePathSegments(pathValue: string): string[] {
3636
return pathValue
@@ -40,7 +40,9 @@ function normalizePathSegments(pathValue: string): string[] {
4040
}
4141

4242
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);
4446
}
4547

4648
function readStat(file: TurnDiffFileChange): TurnDiffStat | null {

0 commit comments

Comments
 (0)