Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -746,21 +735,9 @@ export const fetchActivity = cache(async function fetchActivity(
* 配列の作成とソートを最小限に抑えることでパフォーマンスを向上させます
*/
function getTopK(map: Map<string, number>, 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);
Comment on lines +738 to +740

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are two points to consider here:

  1. Defensive Programming: If k is passed as a negative number, slice(0, k) will slice from the end of the array (e.g., returning all but the last |k| elements), which is incorrect for a "top K" function. Using Math.max(0, k) ensures that a negative k is safely treated as 0 and returns an empty array.
  2. Outdated Documentation: The JSDoc comment above this function (lines 735-736) states that it "improves performance by minimizing array creation and sorting" (配列の作成とソートを最小限に抑えることでパフォーマンスを向上させます). Since the new implementation creates a full array and sorts it entirely, this JSDoc is now misleading and should be updated to reflect the new behavior.
Suggested change
return Array.from(map, ([name, count]) => ({ name, count }))
.sort((a, b) => b.count - a.count)
.slice(0, k);
return Array.from(map, ([name, count]) => ({ name, count }))
.sort((a, b) => b.count - a.count)
.slice(0, Math.max(0, k));

}

/**
Expand Down
Loading