-
Notifications
You must be signed in to change notification settings - Fork 0
feat: [performance improvement] optimize tag filtering array operations #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,8 +40,16 @@ export async function generateMetadata({ params }: { params: Promise<{ tag: stri | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
| const targetTag = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const foundDisplayTag = allTalks.reduce<string | null>((acc, talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (acc) return acc; | ||||||||||||||||||||||||||||||||||||||||||||||
| const talkTags = getTagsFromTalk(talk); | ||||||||||||||||||||||||||||||||||||||||||||||
| const matchedTag = talkTags.find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTag); | ||||||||||||||||||||||||||||||||||||||||||||||
| return matchedTag ?? null; | ||||||||||||||||||||||||||||||||||||||||||||||
| }, null); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = foundDisplayTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| title: `Talks tagged "${displayTag}" - DevBcn ${year}`, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -58,14 +66,21 @@ export default async function Page({ params }: { params: Promise<{ tag: string } | |||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
| const targetTag = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks: typeof allTalks = []; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks = allTalks.filter((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const foundDisplayTag = allTalks.reduce<string | null>((acc, talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const talkTags = getTagsFromTalk(talk); | ||||||||||||||||||||||||||||||||||||||||||||||
| const matchedTag = talkTags.find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTag); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (matchedTag) { | ||||||||||||||||||||||||||||||||||||||||||||||
| filteredTalks.push(talk); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return acc ?? matchedTag ?? null; | ||||||||||||||||||||||||||||||||||||||||||||||
| }, null); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+72
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()); | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = foundDisplayTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (filteredTalks.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| notFound(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -47,8 +47,16 @@ export async function generateMetadata({ params }: Readonly<TagPageProps>): Prom | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
| const targetTag = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const foundDisplayTag = allTalks.reduce<string | null>((acc, talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (acc) return acc; | ||||||||||||||||||||||||||||||||||||||||||||||
| const talkTags = getTagsFromTalk(talk); | ||||||||||||||||||||||||||||||||||||||||||||||
| const matchedTag = talkTags.find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTag); | ||||||||||||||||||||||||||||||||||||||||||||||
| return matchedTag ?? null; | ||||||||||||||||||||||||||||||||||||||||||||||
| }, null); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+57
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = foundDisplayTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| title: `Talks tagged "${displayTag}" - DevBcn ${year}`, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -64,14 +72,21 @@ export default async function TagPage({ params }: Readonly<TagPageProps>) { | |||||||||||||||||||||||||||||||||||||||||||||
| const sessionGroups = await getTalks(year); | ||||||||||||||||||||||||||||||||||||||||||||||
| const allTalks = sessionGroups.flatMap((group) => group.sessions); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = | ||||||||||||||||||||||||||||||||||||||||||||||
| allTalks.flatMap(getTagsFromTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()) ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
| const targetTag = decodedTag.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks: typeof allTalks = []; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const filteredTalks = allTalks.filter((talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const foundDisplayTag = allTalks.reduce<string | null>((acc, talk) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const talkTags = getTagsFromTalk(talk); | ||||||||||||||||||||||||||||||||||||||||||||||
| const matchedTag = talkTags.find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTag); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (matchedTag) { | ||||||||||||||||||||||||||||||||||||||||||||||
| filteredTalks.push(talk); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return acc ?? matchedTag ?? null; | ||||||||||||||||||||||||||||||||||||||||||||||
| }, null); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+78
to
+87
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()); | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
| const displayTag = foundDisplayTag ?? decodedTag.replaceAll("-", " "); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (filteredTalks.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| notFound(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Array.prototype.reducehere does not short-circuit once a match is found, meaning it will continue to iterate through the entireallTalksarray even afteraccis populated. Replacing this with a simplefor...ofloop allows you to break early, improving performance.