Skip to content
Myr

perf(trie-router): avoid calling spread operator for `Object.create(null)` (#3735)

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

* perf(trie-router): avoid calling spread operator for `Object.create(null)` This optimization is based on the discussion in the following Pull Request. Thanks to @EdamAme-x! https://github.com/honojs/hono/pull/3732 Co-authored-by: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> * perf(trie-router): make some #getHandlerSets parameters optional * fix(trie-router): fix #getHandlerSets bug ---------

authored by Taku Amano · committed by GitHub · parent 979510f · Browse files · 1 file+18 added−25 removed
This commit

State

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

Trailers

  • Co-authored-by: EdamAmex <121654029+EdamAme-x@users.noreply.github.com>

Diff stats

1 file+18 added−25 removed

  • other:+18 added−25 removed

1 file+18 added−25 removed

modified:src/router/trie-router/node.ts+18 added-25 removed
11 unmodified lines
12type HandlerParamsSet<T> = HandlerSet<T> & {13 params: Record<string, string>14}1516const emptyParams = Object.create(null)1718export class Node<T> {19 #methods: Record<string, HandlerSet<T>>[]2021 #children: Record<string, Node<T>>22 #patterns: Pattern[]23 #order: number = 022 #params: Record<string, string> = Object.create(null)24 #params: Record<string, string> = emptyParams2526 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {27 this.#children = children || Object.create(null)28 this.#methods = []
54 unmodified lines
83 #getHandlerSets(84 node: Node<T>,85 method: string,86 nodeParams: Record<string, string>,85 params: Record<string, string>87 params?: Record<string, string>88 ): HandlerParamsSet<T>[] {89 const handlerSets: HandlerParamsSet<T>[] = []90 for (let i = 0, len = node.#methods.length; i < len; i++) {91 const m = node.#methods[i]92 const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>93 const processedSet: Record<number, boolean> = {}94 if (handlerSet !== undefined) {95 handlerSet.params = Object.create(null)94 for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {95 const key = handlerSet.possibleKeys[i]96 const processed = processedSet[handlerSet.score]97 handlerSet.params[key] =98 params[key] && !processed ? params[key] : nodeParams[key] ?? params[key]99 processedSet[handlerSet.score] = true100 }10196 handlerSets.push(handlerSet)97 if (nodeParams !== emptyParams || (params && params !== emptyParams)) {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]103 processedSet[handlerSet.score] = true104 }105 }106 }107 }108 return handlerSets109 }110111 search(method: string, path: string): [[T, Params][]] {112 const handlerSets: HandlerParamsSet<T>[] = []110 this.#params = Object.create(null)113 this.#params = emptyParams114115 // eslint-disable-next-line @typescript-eslint/no-this-alias116 const curNode: Node<T> = this117 let curNodes = [curNode]
13 unmodified lines
131 if (isLast) {132 // '/hello/*' => match '/hello'133 if (nextNode.#children['*']) {134 handlerSets.push(132 ...this.#getHandlerSets(133 nextNode.#children['*'],134 method,135 node.#params,136 Object.create(null)137 )135 ...this.#getHandlerSets(nextNode.#children['*'], method, node.#params)136 )137 }140 handlerSets.push(141 ...this.#getHandlerSets(nextNode, method, node.#params, Object.create(null))142 )138 handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params))139 } else {140 tempNodes.push(nextNode)141 }142 }143144 for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {145 const pattern = node.#patterns[k]146 const params = node.#params === emptyParams ? {} : { ...node.#params }147151 const params = { ...node.#params }152148 // Wildcard149 // '/hello/*/foo' => match /hello/bar/foo150 if (pattern === '*') {151 const astNode = node.#children['*']152 if (astNode) {158 handlerSets.push(159 ...this.#getHandlerSets(astNode, method, node.#params, Object.create(null))160 )153 handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params))154 tempNodes.push(astNode)155 }156 continue157 }
46 unmodified lines