⚡ Bolt: Use find instead of filter()[0] for performance improvement#106
⚡ Bolt: Use find instead of filter()[0] for performance improvement#106bartholomej wants to merge 1 commit intomasterfrom
find instead of filter()[0] for performance improvement#106Conversation
Co-authored-by: bartholomej <5861310+bartholomej@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
📝 WalkthroughWalkthroughModified two helper functions with minor optimizations: Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #106 +/- ##
==========================================
- Coverage 99.46% 99.46% -0.01%
==========================================
Files 33 33
Lines 745 743 -2
Branches 186 185 -1
==========================================
- Hits 741 739 -2
Misses 4 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/helpers/global.helper.ts`:
- Around line 10-14: The exported helpers parseIdFromUrl and parseLastIdFromUrl
are declared to return number but actually return null in some branches; update
both function signatures to return number | null, adjust any JSDoc/types and
imports referencing these functions as needed, and ensure the implementations
(parseIdFromUrl, parseLastIdFromUrl) still return either a number or null (using
PARSE_ID_REGEX and the same match logic) so the type accurately reflects
nullable behavior.
| export const parseIdFromUrl = (url: string): number => { | ||
| if (!url) return null; | ||
|
|
||
| const parts = url.split('/'); | ||
| // Detect language prefix like /en/ or /sk/ | ||
| const hasLangPrefix = LANG_PREFIX_REGEX.test(parts[1]); | ||
| const idSlug = parts[hasLangPrefix ? 3 : 2]; | ||
| const id = idSlug?.split('-')[0]; | ||
| return +id || null; | ||
| if (!url || url.startsWith('http')) return null; | ||
|
|
||
| const match = url.match(PARSE_ID_REGEX); | ||
| return match ? +match[1] : null; |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "1) Confirm declaration and nullable returns in parseIdFromUrl"
rg -n -C3 'export const parseIdFromUrl|return null' src/helpers/global.helper.ts
echo
echo "2) Check TypeScript strictness settings that determine whether this mismatch is compile-blocking"
fd 'tsconfig.*json' -x sh -c 'echo "== {} =="; rg -n "\"strict\"|\"strictNullChecks\"" "{}" || true'
echo
echo "3) Check if PARSE_ID_REGEX is defined and understand the full function context"
rg -B5 -A10 'parseIdFromUrl' src/helpers/global.helper.tsRepository: bartholomej/node-csfd-api
Length of output: 1647
Fix parseIdFromUrl return type to match nullable behavior.
The function is typed as number but returns null on multiple branches (lines 11 and 14). Even though strictNullChecks is disabled in tsconfig.json, this type contract is unsafe and inaccurate. The function is a public export and callers relying on the number type contract will encounter runtime nulls.
🔧 Proposed fix
-export const parseIdFromUrl = (url: string): number => {
+export const parseIdFromUrl = (url: string): number | null => {
if (!url || url.startsWith('http')) return null;
const match = url.match(PARSE_ID_REGEX);
return match ? +match[1] : null;
};Note: parseLastIdFromUrl on line 18 has the same issue and should also be fixed to declare number | null.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| export const parseIdFromUrl = (url: string): number => { | |
| if (!url) return null; | |
| const parts = url.split('/'); | |
| // Detect language prefix like /en/ or /sk/ | |
| const hasLangPrefix = LANG_PREFIX_REGEX.test(parts[1]); | |
| const idSlug = parts[hasLangPrefix ? 3 : 2]; | |
| const id = idSlug?.split('-')[0]; | |
| return +id || null; | |
| if (!url || url.startsWith('http')) return null; | |
| const match = url.match(PARSE_ID_REGEX); | |
| return match ? +match[1] : null; | |
| export const parseIdFromUrl = (url: string): number | null => { | |
| if (!url || url.startsWith('http')) return null; | |
| const match = url.match(PARSE_ID_REGEX); | |
| return match ? +match[1] : null; | |
| }; |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/helpers/global.helper.ts` around lines 10 - 14, The exported helpers
parseIdFromUrl and parseLastIdFromUrl are declared to return number but actually
return null in some branches; update both function signatures to return number |
null, adjust any JSDoc/types and imports referencing these functions as needed,
and ensure the implementations (parseIdFromUrl, parseLastIdFromUrl) still return
either a number or null (using PARSE_ID_REGEX and the same match logic) so the
type accurately reflects nullable behavior.
💡 What: Changed
Array.prototype.filter(...)[0]toArray.prototype.find(...)in thegetMovieGrouphelper method insidesrc/helpers/movie.helper.ts.🎯 Why: The original implementation was iterating through the entire list of elements (creators h4 nodes) and creating a new intermediate array just to extract the first matching item. Using
.find()correctly short-circuits the loop on the first match, resulting in fewer iterations and avoiding unnecessary array allocations.📊 Impact: The change reduces iteration time complexity from always$O(N)$ to potentially $O(1)$ best case or average case, scaling better as the number of queried nodes increases, and slightly reduces garbage collection overhead from the discarded intermediate array.
🔬 Measurement: To verify the improvement, run
yarn test --watch=falseto ensure tests complete without functional regressions. The impact is algorithmic (no full iterations) and thus naturally faster on average over large DOM queries.PR created automatically by Jules for task 9914405216872059081 started by @bartholomej
Summary by CodeRabbit