From 7e17b76ce9438d68072a271bd593f7d5deb8542b Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:16:19 +0900 Subject: [PATCH] perf(trie-router): optimize and remove unnecessary processes (#3647) * perf(trie-router): remove unnecessary processes and speed up * revert (4) * fix --- src/router/trie-router/node.ts | 47 ++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 3130433b..2d0ae6c3 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -63,10 +63,6 @@ export class Node { curNode = curNode.children[p] } - if (!curNode.methods.length) { - curNode.methods = [] - } - const m: Record> = Object.create(null) const handlerSet: HandlerSet = { @@ -82,7 +78,7 @@ export class Node { } // getHandlerSets - #gHSets( + #getHandlerSets( node: Node, method: string, nodeParams: Record, @@ -92,7 +88,7 @@ export class Node { for (let i = 0, len = node.methods.length; i < len; i++) { const m = node.methods[i] const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet - const processedSet: Record = Object.create(null) + const processedSet: Record = {} if (handlerSet !== undefined) { handlerSet.params = Object.create(null) for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) { @@ -133,10 +129,17 @@ export class Node { // '/hello/*' => match '/hello' if (nextNode.children['*']) { handlerSets.push( - ...this.#gHSets(nextNode.children['*'], method, node.params, Object.create(null)) + ...this.#getHandlerSets( + nextNode.children['*'], + method, + node.params, + Object.create(null) + ) ) } - handlerSets.push(...this.#gHSets(nextNode, method, node.params, Object.create(null))) + handlerSets.push( + ...this.#getHandlerSets(nextNode, method, node.params, Object.create(null)) + ) } else { tempNodes.push(nextNode) } @@ -152,7 +155,9 @@ export class Node { if (pattern === '*') { const astNode = node.children['*'] if (astNode) { - handlerSets.push(...this.#gHSets(astNode, method, node.params, Object.create(null))) + handlerSets.push( + ...this.#getHandlerSets(astNode, method, node.params, Object.create(null)) + ) tempNodes.push(astNode) } continue @@ -170,24 +175,22 @@ export class Node { const restPathString = parts.slice(i).join('/') if (matcher instanceof RegExp && matcher.test(restPathString)) { params[name] = restPathString - handlerSets.push(...this.#gHSets(child, method, node.params, params)) + handlerSets.push(...this.#getHandlerSets(child, method, node.params, params)) continue } if (matcher === true || matcher.test(part)) { - if (typeof key === 'string') { - params[name] = part - if (isLast) { - handlerSets.push(...this.#gHSets(child, method, params, node.params)) - if (child.children['*']) { - handlerSets.push( - ...this.#gHSets(child.children['*'], method, params, node.params) - ) - } - } else { - child.params = params - tempNodes.push(child) + params[name] = part + if (isLast) { + handlerSets.push(...this.#getHandlerSets(child, method, params, node.params)) + if (child.children['*']) { + handlerSets.push( + ...this.#getHandlerSets(child.children['*'], method, params, node.params) + ) } + } else { + child.params = params + tempNodes.push(child) } } }