-
Notifications
You must be signed in to change notification settings - Fork 0
feat: [performance improvement] optimize tag page filtering #246
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 |
|---|---|---|
|
|
@@ -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 targetTagNormalized = decodedTag.toLowerCase(); | ||
|
|
||
| const matchedTalk = allTalks.find((talk) => { | ||
| const talkTags = getTagsFromTalk(talk); | ||
| return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagNormalized); | ||
| }); | ||
|
|
||
| const displayTag = matchedTalk | ||
| ? (getTagsFromTalk(matchedTalk).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagNormalized) ?? decodedTag.replaceAll("-", " ")) | ||
| : decodedTag.replaceAll("-", " "); | ||
|
Comment on lines
+50
to
+59
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. The current implementation performs redundant operations: it first searches for a matching talk using We can optimize this by using a simple |
||
|
|
||
| return { | ||
| title: `Talks tagged "${displayTag}" - DevBcn ${year}`, | ||
|
|
@@ -64,19 +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 targetTagNormalized = decodedTag.toLowerCase(); | ||
|
|
||
| const filteredTalks = allTalks.filter((talk) => { | ||
| const talkTags = getTagsFromTalk(talk); | ||
|
|
||
| return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === decodedTag.toLowerCase()); | ||
| return talkTags.some((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagNormalized); | ||
| }); | ||
|
|
||
| if (filteredTalks.length === 0) { | ||
| notFound(); | ||
| } | ||
|
|
||
| const displayTag = | ||
| getTagsFromTalk(filteredTalks[0]).find((t) => t.replaceAll(" ", "-").toLowerCase() === targetTagNormalized) ?? decodedTag.replaceAll("-", " "); | ||
|
|
||
| return ( | ||
| <div> | ||
| {/* Header */} | ||
|
|
||
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.
The current implementation performs redundant operations: it first searches for a matching talk using
.find()and.some(), and then searches the tags of that talk again using.find(). This results in duplicate calls togetTagsFromTalkand redundant string replacements/lowercasing.We can optimize this by using a simple
for...ofloop to find the matching tag directly in a single pass, which short-circuits immediately and avoids double-traversal.