From 437c766bcb536fc2ba57e2596939bfe2452904e0 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:45:08 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20refactor:=20optimize=20array=20sort?= =?UTF-8?q?ing=20in=20github.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/lib/github.ts | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/src/lib/github.ts b/src/lib/github.ts index ed7dd37..7cc717a 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -427,20 +427,9 @@ function processRepoData(repos: RepoNode[]): RepositoryData { } const totalBytes = Array.from(languageMap.values()).reduce((a, b) => a + b.bytes, 0); - const topLanguages: { name: string; bytes: number; color: string }[] = []; - for (const [name, data] of languageMap.entries()) { - if (topLanguages.length < 10) { - topLanguages.push({ name, ...data }); - topLanguages.sort((a, b) => b.bytes - a.bytes); - } else if (data.bytes > topLanguages[9].bytes) { - let i = 8; - while (i >= 0 && topLanguages[i].bytes < data.bytes) { - topLanguages[i + 1] = topLanguages[i]; - i--; - } - topLanguages[i + 1] = { name, ...data }; - } - } + const topLanguages = Array.from(languageMap, ([name, data]) => ({ name, ...data })) + .sort((a, b) => b.bytes - a.bytes) + .slice(0, 10); const languages: LanguageStats[] = topLanguages.map(({ name, bytes, color }) => ({ name, @@ -746,21 +735,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); } /**