-
-
Notifications
You must be signed in to change notification settings - Fork 14
⚡ Bolt: [performance improvement] Optimize map callback regex and node search loops #168
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: master
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -147,7 +147,10 @@ export const getMovieOrigins = (el: HTMLElement): string[] => { | |||||||||||||||||||||||||||||
| const originNode = el.querySelector('.origin'); | ||||||||||||||||||||||||||||||
| if (!originNode) return []; | ||||||||||||||||||||||||||||||
| const text = originNode.childNodes[0]?.text || ''; | ||||||||||||||||||||||||||||||
| return text.split('/').map(x => x.trim()).filter(x => x); | ||||||||||||||||||||||||||||||
| return text | ||||||||||||||||||||||||||||||
| .split('/') | ||||||||||||||||||||||||||||||
| .map((x) => x.trim()) | ||||||||||||||||||||||||||||||
| .filter((x) => x); | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const getMovieColorRating = (bodyClasses: string[]): CSFDColorRating => { | ||||||||||||||||||||||||||||||
|
|
@@ -261,10 +264,13 @@ export const getMovieRandomPhoto = (el: HTMLElement | null): string => { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Optimization: Extract regex to prevent redundant compilation in .map() iterations | ||||||||||||||||||||||||||||||
| const CLEAN_TEXT_REGEX = /(\r\n|\n|\r|\t)/gm; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const getMovieTrivia = (el: HTMLElement | null): string[] => { | ||||||||||||||||||||||||||||||
| const triviaNodes = el.querySelectorAll('.article-trivia ul li'); | ||||||||||||||||||||||||||||||
| if (triviaNodes?.length) { | ||||||||||||||||||||||||||||||
| return triviaNodes.map((node) => node.textContent.trim().replace(/(\r\n|\n|\r|\t)/gm, '')); | ||||||||||||||||||||||||||||||
| return triviaNodes.map((node) => node.textContent.trim().replace(CLEAN_TEXT_REGEX, '')); | ||||||||||||||||||||||||||||||
|
Comment on lines
270
to
+273
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. Guard nullable input before querying trivia nodes.
🐛 Proposed fix export const getMovieTrivia = (el: HTMLElement | null): string[] => {
+ if (!el) return null;
const triviaNodes = el.querySelectorAll('.article-trivia ul li');
if (triviaNodes?.length) {
return triviaNodes.map((node) => node.textContent.trim().replace(CLEAN_TEXT_REGEX, ''));
} else {
return null;
}
};As per coding guidelines: "Never assume an element exists. CSFD changes layouts. Use optional chaining 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
@@ -273,7 +279,7 @@ export const getMovieTrivia = (el: HTMLElement | null): string[] => { | |||||||||||||||||||||||||||||
| export const getMovieDescriptions = (el: HTMLElement): string[] => { | ||||||||||||||||||||||||||||||
| return el | ||||||||||||||||||||||||||||||
| .querySelectorAll('.body--plots .plot-full p, .body--plots .plots .plots-item p') | ||||||||||||||||||||||||||||||
| .map((movie) => movie.textContent?.trim().replace(/(\r\n|\n|\r|\t)/gm, '')); | ||||||||||||||||||||||||||||||
| .map((movie) => movie.textContent?.trim().replace(CLEAN_TEXT_REGEX, '')); | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const parseMoviePeople = (el: HTMLElement): CSFDMovieCreator[] => { | ||||||||||||||||||||||||||||||
|
|
@@ -434,7 +440,11 @@ export const getMovieGroup = ( | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const getMovieType = (el: HTMLElement): CSFDFilmTypes => { | ||||||||||||||||||||||||||||||
| const type = el.querySelector('.film-header-name .type'); | ||||||||||||||||||||||||||||||
| const text = type?.innerText?.replace(/[{()}]/g, '').split('\n')[0].trim() || 'film'; | ||||||||||||||||||||||||||||||
| const text = | ||||||||||||||||||||||||||||||
| type?.innerText | ||||||||||||||||||||||||||||||
| ?.replace(/[{()}]/g, '') | ||||||||||||||||||||||||||||||
| .split('\n')[0] | ||||||||||||||||||||||||||||||
| .trim() || 'film'; | ||||||||||||||||||||||||||||||
| return parseFilmType(text); | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,4 +96,3 @@ export const csfd = new Csfd( | |
| ); | ||
|
|
||
| export type * from './dto'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,8 @@ | ||
| export * from "./dto/cinema"; | ||
| export * from "./dto/creator"; | ||
| export * from "./dto/global"; | ||
| export * from "./dto/movie"; | ||
| export * from "./dto/options"; | ||
| export * from "./dto/search"; | ||
| export * from "./dto/user-ratings"; | ||
| export * from "./dto/user-reviews"; | ||
|
|
||
|
|
||
| export * from './dto/cinema'; | ||
| export * from './dto/creator'; | ||
| export * from './dto/global'; | ||
| export * from './dto/movie'; | ||
| export * from './dto/options'; | ||
| export * from './dto/search'; | ||
| export * from './dto/user-ratings'; | ||
| export * from './dto/user-reviews'; |
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.
Guard against
"undefined ratings"in CLI output.At Line 49,
movie.ratingCount?.toLocaleString()can printundefinedwhenmovie.ratingexists butmovie.ratingCountis missing. Add a fallback before interpolation.💡 Suggested fix
🤖 Prompt for AI Agents