Skip to content
TypeScript

perf(shared): avoid repeated date parsing when selecting PRs (#22891)

Utkarsh Patil87d22b00f75a
diff --git a/packages/client-runtime/src/threadPullRequests.test.ts b/packages/client-runtime/src/threadPullRequests.test.ts
index 11bd05de90d..9867d776b07 100644
--- a/packages/client-runtime/src/threadPullRequests.test.ts
+++ b/packages/client-runtime/src/threadPullRequests.test.ts
@@ -104,6 +104,55 @@ describe("threadPullRequestKeysEqual", () => {
});
describe("resolveThreadCurrentPullRequest", () => {
+ it("selects the same terminal link as stable sorting across input orders", () => {
+ const dates = ["invalid", "1969-01-01T00:00:00.000Z", "2026-01-01T00:00:00.000Z"];
+ for (let offset = 0; offset < dates.length; offset++) {
+ const links = Array.from({ length: 12 }, (_, index) =>
+ link(index + 1, {
+ snapshot: snapshot({
+ state: "merged",
+ updatedAt: dates[(index + offset) % dates.length]!,
+ }),
+ }),
+ );
+ for (const input of [links, [...links].sort((left, right) => right.number - left.number)]) {
+ const expected = [...input].sort(
+ (left, right) =>
+ (Date.parse(right.snapshot?.updatedAt ?? right.linkedAt) || 0) -
+ (Date.parse(left.snapshot?.updatedAt ?? left.linkedAt) || 0),
+ )[0];
+ expect(resolveThreadCurrentPullRequest(input)).toEqual({ kind: "single", link: expected });
+ }
+ }
+ });
+
+ it("preserves input order when open chains have equal link dates", () => {
+ const linkedAt = "2026-01-01T00:00:00.000Z";
+ const current = resolveThreadCurrentPullRequest([
+ link(3, { linkedAt }),
+ link(1, { linkedAt }),
+ link(2, { linkedAt }),
+ ]);
+ expect(current?.kind === "stack" ? current.open.map((entry) => entry.number) : null).toEqual([
+ 3, 1, 2,
+ ]);
+ });
+
+ it("keeps the first terminal link on ties and treats invalid update dates as zero", () => {
+ const first = link(3, { snapshot: snapshot({ state: "merged", updatedAt: "invalid" }) });
+ const second = link(1, {
+ snapshot: snapshot({ state: "closed", updatedAt: "1970-01-01T00:00:00.000Z" }),
+ });
+ expect(resolveThreadCurrentPullRequest([first, second])).toEqual({
+ kind: "single",
+ link: first,
+ });
+ expect(resolveThreadCurrentPullRequest([second, first])).toEqual({
+ kind: "single",
+ link: second,
+ });
+ });
+
it("prefers the single open link over terminal ones", () => {
const current = resolveThreadCurrentPullRequest([
link(1, { snapshot: snapshot({ state: "merged" }) }),
diff --git a/packages/source-control-core/src/shared/threadPullRequests.ts b/packages/source-control-core/src/shared/threadPullRequests.ts
index c92b768669d..bed52f5ecb8 100644
--- a/packages/source-control-core/src/shared/threadPullRequests.ts
+++ b/packages/source-control-core/src/shared/threadPullRequests.ts
@@ -151,22 +151,33 @@ export function makeThreadPullRequests(
if (open.length > 1) {
const openChains = chains
.map((chain) => layersFromTop(chain).filter(isOpen))
- .filter((layers) => layers.length > 0)
- .sort(
- (left, right) =>
- Math.max(...right.map((link) => Date.parse(link.linkedAt))) -
- Math.max(...left.map((link) => Date.parse(link.linkedAt))),
- );
- const ordered = openChains.flat();
+ .filter((layers) => layers.length > 0);
+ const ordered =
+ openChains.length === 1
+ ? openChains[0]!
+ : openChains
+ .map((layers) => ({
+ layers,
+ newestLinkedAt: Math.max(...layers.map((link) => Date.parse(link.linkedAt))),
+ }))
+ .sort((left, right) => right.newestLinkedAt - left.newestLinkedAt)
+ .flatMap((chain) => chain.layers);
return { kind: "stack", open: ordered, top: ordered[0]! };
}
if (chains.length === 1) {
return { kind: "single", link: layersFromTop(chains[0]!)[0]! };
}
- const terminal = [...visible].sort(
- (left, right) => latestUpdatedAt(right) - latestUpdatedAt(left),
- );
- return { kind: "single", link: terminal[0]! };
+ let latest = visible[0]!;
+ let latestTimestamp = latestUpdatedAt(latest);
+ for (let index = 1; index < visible.length; index++) {
+ const link = visible[index]!;
+ const timestamp = latestUpdatedAt(link);
+ if (timestamp > latestTimestamp) {
+ latest = link;
+ latestTimestamp = timestamp;
+ }
+ }
+ return { kind: "single", link: latest };
}
function resolveThreadCurrentPullRequestLink(