Skip to content
Myr

perf(router): improve performance of router (#3526)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* perf(router): improve performance of router * coverage

authored by EdamAmex · committed by GitHub · parent 234b083 · Browse files · 6 files+123 added−105 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+123 added−105 removed

  • tests:+13 added−0 removed
  • other:+110 added−105 removed

6 files+123 added−105 removed

modified:src/router/linear-router/router.test.ts+13 added
modified:src/router/linear-router/router.ts+91 added-85 removed
modified:src/router/pattern-router/router.ts+4 added-2 removed
38 unmodified lines
3940 match(method: string, path: string): Result<T> {41 const handlers: [T, Params][] = []4243 for (const [pattern, routeMethod, handler] of this.routes) {44 if (routeMethod === METHOD_NAME_ALL || routeMethod === method) {43 for (let i = 0, len = this.routes.length; i < len; i++) {44 const [pattern, routeMethod, handler] = this.routes[i]4546 if (routeMethod === method || routeMethod === METHOD_NAME_ALL) {47 const match = pattern.exec(path)48 if (match) {49 handlers.push([handler, match.groups || Object.create(null)])50 }
6 unmodified lines
modified:src/router/smart-router/router.ts+3 added-3 removed
28 unmodified lines
29 let res30 for (; i < len; i++) {31 const router = routers[i]32 try {33 routes.forEach((args) => {34 router.add(...args)35 })33 for (let i = 0, len = routes.length; i < len; i++) {34 router.add(...routes[i])35 }36 res = router.match(method, path)37 } catch (e) {38 if (e instanceof UnsupportedPathError) {39 continue
29 unmodified lines
modified:src/router/trie-router/node.ts+10 added-13 removed
5 unmodified lines
6type HandlerSet<T> = {7 handler: T8 possibleKeys: string[]9 score: number10 name: string // For debug10}1112type HandlerParamsSet<T> = HandlerSet<T> & {13 params: Record<string, string>
4 unmodified lines
1819 children: Record<string, Node<T>>20 patterns: Pattern[]21 order: number = 023 name: string22 params: Record<string, string> = Object.create(null)2324 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {25 this.children = children || Object.create(null)26 this.methods = []29 this.name = ''27 if (method && handler) {28 const m: Record<string, HandlerSet<T>> = Object.create(null)32 m[method] = { handler, possibleKeys: [], score: 0, name: this.name }29 m[method] = { handler, possibleKeys: [], score: 0 }30 this.methods = [m]31 }32 this.patterns = []33 }3435 insert(method: string, path: string, handler: T): Node<T> {39 this.name = `${method} ${path}`36 this.order = ++this.order3738 // eslint-disable-next-line @typescript-eslint/no-this-alias39 let curNode: Node<T> = this
31 unmodified lines
7172 const handlerSet: HandlerSet<T> = {73 handler,74 possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),79 name: this.name,75 score: this.order,76 }7778 m[method] = handlerSet
15 unmodified lines
94 const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>95 const processedSet: Record<string, boolean> = Object.create(null)96 if (handlerSet !== undefined) {97 handlerSet.params = Object.create(null)103 handlerSet.possibleKeys.forEach((key) => {104 const processed = processedSet[handlerSet.name]98 for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {99 const key = handlerSet.possibleKeys[i]100 const processed = processedSet[handlerSet.score]101 handlerSet.params[key] =102 params[key] && !processed ? params[key] : nodeParams[key] ?? params[key]107 processedSet[handlerSet.name] = true108 })103 processedSet[handlerSet.score] = true104 }105106 handlerSets.push(handlerSet)107 }108 }109 return handlerSets
18 unmodified lines
128 const nextNode = node.children[part]129130 if (nextNode) {131 nextNode.params = node.params135 if (isLast === true) {132 if (isLast) {133 // '/hello/*' => match '/hello'134 if (nextNode.children['*']) {135 handlerSets.push(136 ...this.gHSets(nextNode.children['*'], method, node.params, Object.create(null))
36 unmodified lines
173 handlerSets.push(...this.gHSets(child, method, node.params, params))174 continue175 }176180 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {177 if (matcher === true || matcher.test(part)) {178 if (typeof key === 'string') {179 params[name] = part183 if (isLast === true) {180 if (isLast) {181 handlerSets.push(...this.gHSets(child, method, params, node.params))182 if (child.children['*']) {183 handlerSets.push(...this.gHSets(child.children['*'], method, params, node.params))184 }
18 unmodified lines
modified:src/router/trie-router/router.ts+2 added-2 removed
11 unmodified lines
1213 add(method: string, path: string, handler: T) {14 const results = checkOptionalParameter(path)15 if (results) {16 for (const p of results) {17 this.node.insert(method, p, handler)16 for (let i = 0, len = results.length; i < len; i++) {17 this.node.insert(method, results[i], handler)18 }19 return20 }21
7 unmodified lines