From 18d062e9b3c7ba31e330ef9a7cd9db2a70fe4410 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 10 Jul 2026 06:40:41 +0000 Subject: [PATCH 1/2] perf: optimize getTopK with standard array operations Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/lib/github.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/lib/github.ts b/src/lib/github.ts index ed7dd37..1e5e991 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -746,21 +746,9 @@ export const fetchActivity = cache(async function fetchActivity( * 配列の作成とソートを最小限に抑えることでパフォーマンスを向上させます */ function getTopK(map: Map, k: number = 10): { name: string; count: number }[] { - const top: { name: string; count: number }[] = []; - for (const [name, count] of map.entries()) { - if (top.length < k) { - top.push({ name, count }); - top.sort((a, b) => b.count - a.count); - } else if (count > top[k - 1].count) { - let i = k - 2; - while (i >= 0 && top[i].count < count) { - top[i + 1] = top[i]; - i--; - } - top[i + 1] = { name, count }; - } - } - return top; + return Array.from(map, ([name, count]) => ({ name, count })) + .sort((a, b) => b.count - a.count) + .slice(0, k); } /** From 46989007e66fa6eb6ce3dd936a0cdfe0f38013ad Mon Sep 17 00:00:00 2001 From: is0692vs Date: Mon, 13 Jul 2026 10:38:28 +0900 Subject: [PATCH 2/2] fix: clamp top-k result limit --- src/lib/github.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/github.ts b/src/lib/github.ts index 1e5e991..d35c21b 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -743,12 +743,13 @@ export const fetchActivity = cache(async function fetchActivity( /** * 効率的に Map から上位 K 件を抽出するヘルパー関数 - * 配列の作成とソートを最小限に抑えることでパフォーマンスを向上させます + * 上位 K 件だけを返します。 */ function getTopK(map: Map, k: number = 10): { name: string; count: number }[] { + const limit = Math.max(0, k); return Array.from(map, ([name, count]) => ({ name, count })) .sort((a, b) => b.count - a.count) - .slice(0, k); + .slice(0, limit); } /**