Skip to content

perf: optimize duplicate handler filtering#4003

Merged
pi0 merged 3 commits intonitrojs:mainfrom
OskarLebuda:fix/route-scan
Feb 5, 2026
Merged

perf: optimize duplicate handler filtering#4003
pi0 merged 3 commits intonitrojs:mainfrom
OskarLebuda:fix/route-scan

Conversation

@OskarLebuda
Copy link
Contributor

@OskarLebuda OskarLebuda commented Feb 5, 2026

🔗 Linked issue

n/a

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

The current implementation uses findIndex inside filter, which scans the entire array for each handler - resulting in O(n2) complexity.

Replaced with a Set-based approach for O(1) lookups instead of repeatedly scanning the array. Behavior stays the same - keeps first occurrence, removes duplicates.

For large applications with many routes, this should provide noticeable performance improvement during startup.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@OskarLebuda OskarLebuda requested a review from pi0 as a code owner February 5, 2026 21:51
@vercel
Copy link

vercel bot commented Feb 5, 2026

@OskarLebuda is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Feb 5, 2026

📝 Walkthrough

Walkthrough

The deduplication logic in scanHandlers is updated to use key-based deduplication with a Set instead of index-based deduplication. A composite key combining route, method, and env is created for each handler, ensuring only the first occurrence of each unique key is included in nitro.scannedHandlers.

Changes

Cohort / File(s) Summary
Deduplication refactor
src/scan.ts
Replaced index-based deduplication with Set-based key deduplication using a composite key (route, method, env) to prevent duplicate handler emissions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'perf: optimize duplicate handler filtering' follows conventional commits format with a 'perf' type prefix and describes the optimization change accurately.
Description check ✅ Passed The description clearly explains the performance optimization (O(n²) to O(1) approach), the problem it solves, and maintains the same behavior while improving startup performance.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 5, 2026

Open in StackBlitz

npm i https://pkg.pr.new/nitrojs/nitro@4003

commit: 747ee21

@pi0 pi0 changed the title perf: Optimize duplicate handler filtering perf: optimize duplicate handler filtering Feb 5, 2026
Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thnx!

@pi0 pi0 merged commit f5cf688 into nitrojs:main Feb 5, 2026
10 of 11 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Feb 13, 2026
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants