diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 94ab4de19..a75bda28d 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -13,6 +13,13 @@ type HandlerParamsSet = HandlerSet & { params: Record } +const emptyParams = Object.create(null) +const optimizedEmptyParams = (() => { + const E = function () {} + E.prototype = emptyParams + return E +})() as unknown as { new (): any } + export class Node { #methods: Record>[] @@ -90,7 +97,7 @@ export class Node { const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet const processedSet: Record = {} if (handlerSet !== undefined) { - handlerSet.params = Object.create(null) + handlerSet.params = new optimizedEmptyParams() for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) { const key = handlerSet.possibleKeys[i] const processed = processedSet[handlerSet.score] @@ -107,7 +114,7 @@ export class Node { search(method: string, path: string): [[T, Params][]] { const handlerSets: HandlerParamsSet[] = [] - this.#params = Object.create(null) + this.#params = new optimizedEmptyParams() // eslint-disable-next-line @typescript-eslint/no-this-alias const curNode: Node = this @@ -129,17 +136,10 @@ export class Node { // '/hello/*' => match '/hello' if (nextNode.#children['*']) { handlerSets.push( - ...this.#getHandlerSets( - nextNode.#children['*'], - method, - node.#params, - Object.create(null) - ) + ...this.#getHandlerSets(nextNode.#children['*'], method, node.#params, emptyParams) ) } - handlerSets.push( - ...this.#getHandlerSets(nextNode, method, node.#params, Object.create(null)) - ) + handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params, emptyParams)) } else { tempNodes.push(nextNode) } @@ -155,9 +155,7 @@ export class Node { if (pattern === '*') { const astNode = node.#children['*'] if (astNode) { - handlerSets.push( - ...this.#getHandlerSets(astNode, method, node.#params, Object.create(null)) - ) + handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params, emptyParams)) tempNodes.push(astNode) } continue