From 815922b9cf6bf7595dfa7ba9e8ec02b5ee7d88ce Mon Sep 17 00:00:00 2001 From: miccy <9729864+miccy@users.noreply.github.com> Date: Mon, 20 Apr 2026 08:04:41 +0000 Subject: [PATCH] perf: optimize github contribution scraper complexity - Replace O(N^2) Array.some() with O(N) Set.has() in parseContributionCalendar - Replace O(N^2) filter/findIndex with O(N) Set-based filter in fetchContributionsHTML - Export parseContributionCalendar as internal for benchmarking/testing --- packages/github/src/index.ts | 185 +++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 86 deletions(-) diff --git a/packages/github/src/index.ts b/packages/github/src/index.ts index 9db4fda..d9046b2 100644 --- a/packages/github/src/index.ts +++ b/packages/github/src/index.ts @@ -173,6 +173,99 @@ function levelToNumber(level: ContributionDay["contributionLevel"]): 0 | 1 | 2 | // HTML Scraping (public, no auth) // ============================================ +/** @internal */ +export function parseContributionCalendar( + html: string, + username: string, + year: number, +): ContributionGrid { + const weeks: GitHubContribution[][] = []; + let currentWeek: GitHubContribution[] = []; + let totalContributions = 0; + + // Match all contribution day cells + // GitHub uses: + // We use a robust regex that captures date, count (optional), and level + const dayPattern = + /]*data-date="(\d{4}-\d{2}-\d{2})"[^>]*?(?:data-count="(\d+)"[^>]*?)?data-level="(-?\d+)"[^>]*>/g; + const days: Array<{ date: string; level: number; count?: number }> = []; + const seenDates = new Set(); + + for (const match of html.matchAll(dayPattern)) { + const date = match[1]; + days.push({ + date, + level: Number.parseInt(match[3], 10), + count: match[2] ? Number.parseInt(match[2], 10) : undefined, + }); + seenDates.add(date); + } + + // Also try the alternate pattern (data-level before data-date) + const altPattern = + /]*data-level="(-?\d+)"[^>]*?(?:data-count="(\d+)"[^>]*?)?data-date="(\d{4}-\d{2}-\d{2})"[^>]*>/g; + for (const match of html.matchAll(altPattern)) { + // Avoid duplicates + // Capturing groups: 1=level, 2=count(opt), 3=date + const date = match[3]; + if (!seenDates.has(date)) { + days.push({ + date, + level: Number.parseInt(match[1], 10), + count: match[2] ? Number.parseInt(match[2], 10) : undefined, + }); + seenDates.add(date); + } + } + + // Sort + days.sort((a, b) => a.date.localeCompare(b.date)); + + // Build grid with Sunday alignment + if (days.length > 0) { + // Parse date in UTC to ensure consistent day of week + const [y, m, d] = days[0].date.split("-").map(Number); + const dayOfWeek = new Date(Date.UTC(y, m - 1, d)).getUTCDay(); // 0 = Sunday + + // Pad beginning + for (let i = 0; i < dayOfWeek; i++) { + currentWeek.push({ date: "", count: 0, level: 0 }); + } + } + + for (const day of days) { + const level = Math.max(0, Math.min(4, day.level)) as 0 | 1 | 2 | 3 | 4; + const count = day.count ?? contributionCountFromLevel(level); + + totalContributions += count; + currentWeek.push({ + date: day.date, + count, + level, + }); + + if (currentWeek.length === 7) { + weeks.push(currentWeek); + currentWeek = []; + } + } + + if (currentWeek.length > 0) { + // Pad end + while (currentWeek.length < 7) { + currentWeek.push({ date: "", count: 0, level: 0 }); + } + weeks.push(currentWeek); + } + + return { + username, + year, + totalContributions, + weeks, + }; +} + // Helper to fetch a specific calendar year async function fetchYear(username: string, year: number): Promise { const url = `https://github.com/users/${username}/contributions?from=${year}-01-01&to=${year}-12-31`; @@ -242,9 +335,12 @@ async function fetchContributionsHTML(username: string, year?: number): Promise< allDays.sort((a, b) => a.date.localeCompare(b.date)); // 3. Remove duplicates (overlap at year boundary?) - const uniqueDays = allDays.filter( - (day, index, self) => index === self.findIndex((d) => d.date === day.date), - ); + const seenDates = new Set(); + const uniqueDays = allDays.filter((day) => { + if (seenDates.has(day.date)) return false; + seenDates.add(day.date); + return true; + }); // 4. Slice last 365 days (approx 53 weeks) // We want the graph to end TODAY. @@ -321,89 +417,6 @@ function contributionCountFromLevel(level: number): number { return level * 3; } -function parseContributionCalendar(html: string, username: string, year: number): ContributionGrid { - const weeks: GitHubContribution[][] = []; - let currentWeek: GitHubContribution[] = []; - let totalContributions = 0; - - // Match all contribution day cells - // GitHub uses: - // We use a robust regex that captures date, count (optional), and level - const dayPattern = - /]*data-date="(\d{4}-\d{2}-\d{2})"[^>]*?(?:data-count="(\d+)"[^>]*?)?data-level="(-?\d+)"[^>]*>/g; - const days: Array<{ date: string; level: number; count?: number }> = []; - - for (const match of html.matchAll(dayPattern)) { - days.push({ - date: match[1], - level: Number.parseInt(match[3], 10), - count: match[2] ? Number.parseInt(match[2], 10) : undefined, - }); - } - - // Also try the alternate pattern (data-level before data-date) - const altPattern = - /]*data-level="(-?\d+)"[^>]*?(?:data-count="(\d+)"[^>]*?)?data-date="(\d{4}-\d{2}-\d{2})"[^>]*>/g; - for (const match of html.matchAll(altPattern)) { - // Avoid duplicates - // Capturing groups: 1=level, 2=count(opt), 3=date - const date = match[3]; - if (!days.some((d) => d.date === date)) { - days.push({ - date, - level: Number.parseInt(match[1], 10), - count: match[2] ? Number.parseInt(match[2], 10) : undefined, - }); - } - } - - // Sort - days.sort((a, b) => a.date.localeCompare(b.date)); - - // Build grid with Sunday alignment - if (days.length > 0) { - // Parse date in UTC to ensure consistent day of week - const [y, m, d] = days[0].date.split("-").map(Number); - const dayOfWeek = new Date(Date.UTC(y, m - 1, d)).getUTCDay(); // 0 = Sunday - - // Pad beginning - for (let i = 0; i < dayOfWeek; i++) { - currentWeek.push({ date: "", count: 0, level: 0 }); - } - } - - for (const day of days) { - const level = Math.max(0, Math.min(4, day.level)) as 0 | 1 | 2 | 3 | 4; - const count = day.count ?? contributionCountFromLevel(level); - - totalContributions += count; - currentWeek.push({ - date: day.date, - count, - level, - }); - - if (currentWeek.length === 7) { - weeks.push(currentWeek); - currentWeek = []; - } - } - - if (currentWeek.length > 0) { - // Pad end - while (currentWeek.length < 7) { - currentWeek.push({ date: "", count: 0, level: 0 }); - } - weeks.push(currentWeek); - } - - return { - username, - year, - totalContributions, - weeks, - }; -} // ============================================ // Exports