perf(trie-router): optimize and remove unnecessary processes (#3647)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* perf(trie-router): remove unnecessary processes and speed up * revert (4) * fix
authored by EdamAmex · committed by GitHub · parent 61bee9f · Browse files · 1 file·+25 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+25 added−22 removed
- other:+25 added−22 removed
Provenance
- Commit
7e17b76- Tree
e81aa3f- Author
EdamAmex <121654029+EdamAme-x@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
61bee9f
1 file changed+25 added−22 removed
modified:src/router/trie-router/node.ts+25 added-22 removed
61 unmodified lines62 }63 curNode = curNode.children[p]64 }65 66- if (!curNode.methods.length) {67- curNode.methods = []68- }69-66 const m: Record<string, HandlerSet<T>> = Object.create(null)67 68 const handlerSet: HandlerSet<T> = {69 handler,7 unmodified lines77 return curNode78 }79 80 // getHandlerSets85- #gHSets(81+ #getHandlerSets(82 node: Node<T>,83 method: string,84 nodeParams: Record<string, string>,85 params: Record<string, string>1 unmodified line87 const handlerSets: HandlerParamsSet<T>[] = []88 for (let i = 0, len = node.methods.length; i < len; i++) {89 const m = node.methods[i]90 const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>95- const processedSet: Record<string, boolean> = Object.create(null)91+ const processedSet: Record<number, boolean> = {}92 if (handlerSet !== undefined) {93 handlerSet.params = Object.create(null)94 for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {95 const key = handlerSet.possibleKeys[i]32 unmodified lines128 if (isLast) {129 // '/hello/*' => match '/hello'130 if (nextNode.children['*']) {131 handlerSets.push(136- ...this.#gHSets(nextNode.children['*'], method, node.params, Object.create(null))132+ ...this.#getHandlerSets(133+ nextNode.children['*'],134+ method,135+ node.params,136+ Object.create(null)137+ )138 )139 }139- handlerSets.push(...this.#gHSets(nextNode, method, node.params, Object.create(null)))140+ handlerSets.push(141+ ...this.#getHandlerSets(nextNode, method, node.params, Object.create(null))142+ )143 } else {144 tempNodes.push(nextNode)145 }146 }7 unmodified lines154 // '/hello/*/foo' => match /hello/bar/foo155 if (pattern === '*') {156 const astNode = node.children['*']157 if (astNode) {155- handlerSets.push(...this.#gHSets(astNode, method, node.params, Object.create(null)))158+ handlerSets.push(159+ ...this.#getHandlerSets(astNode, method, node.params, Object.create(null))160+ )161 tempNodes.push(astNode)162 }163 continue164 }9 unmodified lines174 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js175 const restPathString = parts.slice(i).join('/')176 if (matcher instanceof RegExp && matcher.test(restPathString)) {177 params[name] = restPathString173- handlerSets.push(...this.#gHSets(child, method, node.params, params))178+ handlerSets.push(...this.#getHandlerSets(child, method, node.params, params))179 continue180 }181 182 if (matcher === true || matcher.test(part)) {178- if (typeof key === 'string') {179- params[name] = part180- if (isLast) {181- handlerSets.push(...this.#gHSets(child, method, params, node.params))182- if (child.children['*']) {183- handlerSets.push(184- ...this.#gHSets(child.children['*'], method, params, node.params)185- )183+ params[name] = part184+ if (isLast) {185+ handlerSets.push(...this.#getHandlerSets(child, method, params, node.params))186+ if (child.children['*']) {187+ handlerSets.push(188+ ...this.#getHandlerSets(child.children['*'], method, params, node.params)189+ )186- }187- } else {188- child.params = params189- tempNodes.push(child)190 }191+ } else {192+ child.params = params193+ tempNodes.push(child)194 }195 }196 }197 }10 unmodified lines