Skip to content

Commit 350e248

Browse files
committed
refactor: improve perf for looking up routes
1 parent c9d3445 commit 350e248

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

src/router/lookup_store/main.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ import type { Qs } from '../../qs.js'
1313
import { UrlBuilder } from './url_builder.js'
1414
import { RouteFinder } from './route_finder.js'
1515
import type { RouteJSON } from '../../types/route.js'
16+
import { E_CANNOT_LOOKUP_ROUTE } from '../../exceptions.js'
1617

1718
/**
1819
* Lookup store exposes the API to lookup routes and
1920
* make URLs for registered routes.
2021
*/
2122
export class LookupStore {
2223
/**
23-
* List of routes grouped by domain
24+
* List of route finders grouped by domains
2425
*/
25-
#routes: { [domain: string]: RouteJSON[] } = {}
26+
#routes: { [domain: string]: RouteFinder } = {}
2627

2728
/**
2829
* Encryption for making URLs
@@ -43,8 +44,8 @@ export class LookupStore {
4344
* Register route JSON payload
4445
*/
4546
register(route: RouteJSON) {
46-
this.#routes[route.domain] = this.#routes[route.domain] || []
47-
this.#routes[route.domain].push(route)
47+
this.#routes[route.domain] = this.#routes[route.domain] || new RouteFinder()
48+
this.#routes[route.domain].register(route)
4849
}
4950

5051
/**
@@ -60,8 +61,8 @@ export class LookupStore {
6061
* domain.
6162
*/
6263
builderForDomain(domain: string) {
63-
const routes = this.#routes[domain]
64-
return new UrlBuilder(this.#encryption, new RouteFinder(routes || []), this.#qsParser)
64+
const finder = this.#routes[domain]
65+
return new UrlBuilder(this.#encryption, finder || new RouteFinder(), this.#qsParser)
6566
}
6667

6768
/**
@@ -70,8 +71,12 @@ export class LookupStore {
7071
* itself.
7172
*/
7273
find(routeIdentifier: string, domain?: string): RouteJSON | null {
73-
const routes = this.#routes[domain || 'root'] || []
74-
return new RouteFinder(routes).find(routeIdentifier)
74+
const finder = this.#routes[domain || 'root']
75+
if (!finder) {
76+
return null
77+
}
78+
79+
return finder.find(routeIdentifier)
7580
}
7681

7782
/**
@@ -82,8 +87,12 @@ export class LookupStore {
8287
* An error is raised when unable to find the route.
8388
*/
8489
findOrFail(routeIdentifier: string, domain?: string): RouteJSON {
85-
const routes = this.#routes[domain || 'root'] || []
86-
return new RouteFinder(routes).findOrFail(routeIdentifier)
90+
const finder = this.#routes[domain || 'root']
91+
if (!finder) {
92+
throw new E_CANNOT_LOOKUP_ROUTE([routeIdentifier])
93+
}
94+
95+
return finder.findOrFail(routeIdentifier)
8796
}
8897

8998
/**
@@ -92,11 +101,18 @@ export class LookupStore {
92101
* itself.
93102
*/
94103
has(routeIdentifier: string, domain?: string): boolean {
95-
const routes = this.#routes[domain || 'root'] || []
96-
return new RouteFinder(routes).has(routeIdentifier)
104+
const finder = this.#routes[domain || 'root']
105+
if (!finder) {
106+
return false
107+
}
108+
109+
return finder.has(routeIdentifier)
97110
}
98111

99112
toJSON() {
100-
return this.#routes
113+
return Object.keys(this.#routes).reduce<Record<string, RouteJSON[]>>((result, domain) => {
114+
result[domain] = this.#routes[domain].toJSON()
115+
return result
116+
}, {})
101117
}
102118
}

src/router/lookup_store/route_finder.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import type { RouteJSON } from '../../types/route.js'
1515
* or the controller.method name.
1616
*/
1717
export class RouteFinder {
18-
#routes: RouteJSON[]
19-
constructor(routes: RouteJSON[]) {
20-
this.#routes = routes
18+
#routes: RouteJSON[] = []
19+
20+
register(route: RouteJSON) {
21+
this.#routes.push(route)
2122
}
2223

2324
/**
@@ -57,4 +58,11 @@ export class RouteFinder {
5758
has(routeIdentifier: string): boolean {
5859
return !!this.find(routeIdentifier)
5960
}
61+
62+
/**
63+
* Returns an array of registered routes
64+
*/
65+
toJSON() {
66+
return this.#routes
67+
}
6068
}

0 commit comments

Comments
 (0)