feat(router): Introduce `OptimizeRouter`
A router that caches static routes to objects.
#routes: Record<string, Record<string, number[]>> = Object.create(null)
Like SmartRouter, it can specify child routers.
new OptimizeRouter({
router: new TrieRouter(),
})
... (wait a moment)
The author should do the following, if applicable
Codecov Report
Attention: Patch coverage is 97.95918% with 1 line in your changes missing coverage. Please review.
Project coverage is 91.73%. Comparing base (
fc8fb0f) to head (b54760d).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/router/optimize-router/index.ts | 0.00% | 1 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #3726 +/- ##
==========================================
+ Coverage 91.70% 91.73% +0.02%
==========================================
Files 159 161 +2
Lines 10165 10214 +49
Branches 2981 2890 -91
==========================================
+ Hits 9322 9370 +48
- Misses 842 843 +1
Partials 1 1
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
@EdamAme-x U should use a Map instead
Objects are only fast if the request handler doesn't access unknown paths, which then the property access gets optimized out.
In my benchmarks, using an Object was faster than using a Map.
@EdamAme-x Can u show me the benchmark source?
I have my own router benchmark in which routers that use Map for static paths are slightly faster in Node and slightly slower in Bun (Likely cuz JSC lacks optimizations for Map).
https://github.com/mapljs/router/tree/main/bench/routers
One potential issue that can occur in your benchmark is that your object isn't deoptimized. For example if you only access known paths:
const routes = {
'/': 0,
'/user': 1
};
// Fast access using hidden class
routes['/'];
routes['/user'];
But if you access unknown paths at some point (10 unknown props I think) the engine will stop creating a hidden class and treat it like a hashmap, which access slower. So you have to warm up the benchmark using unknown keys first.