Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions src/router/trie-router/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ type HandlerParamsSet<T> = HandlerSet<T> & {
params: Record<string, string>
}

const emptyParams = Object.create(null)

export class Node<T> {
#methods: Record<string, HandlerSet<T>>[]

#children: Record<string, Node<T>>
#patterns: Pattern[]
#order: number = 0
#params: Record<string, string> = Object.create(null)
#params: Record<string, string> = emptyParams

constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {
this.#children = children || Object.create(null)
Expand Down Expand Up @@ -82,7 +84,7 @@ export class Node<T> {
node: Node<T>,
method: string,
nodeParams: Record<string, string>,
params: Record<string, string>
params?: Record<string, string>
): HandlerParamsSet<T>[] {
const handlerSets: HandlerParamsSet<T>[] = []
for (let i = 0, len = node.#methods.length; i < len; i++) {
Expand All @@ -91,23 +93,24 @@ export class Node<T> {
const processedSet: Record<number, boolean> = {}
if (handlerSet !== undefined) {
handlerSet.params = Object.create(null)
for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {
const key = handlerSet.possibleKeys[i]
const processed = processedSet[handlerSet.score]
handlerSet.params[key] =
params[key] && !processed ? params[key] : nodeParams[key] ?? params[key]
processedSet[handlerSet.score] = true
}

handlerSets.push(handlerSet)
if (nodeParams !== emptyParams || (params && params !== emptyParams)) {
for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {
const key = handlerSet.possibleKeys[i]
const processed = processedSet[handlerSet.score]
handlerSet.params[key] =
params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key]
processedSet[handlerSet.score] = true
}
}
}
}
return handlerSets
}

search(method: string, path: string): [[T, Params][]] {
const handlerSets: HandlerParamsSet<T>[] = []
this.#params = Object.create(null)
this.#params = emptyParams

// eslint-disable-next-line @typescript-eslint/no-this-alias
const curNode: Node<T> = this
Expand All @@ -129,35 +132,25 @@ export class Node<T> {
// '/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)
)
}
handlerSets.push(
...this.#getHandlerSets(nextNode, method, node.#params, Object.create(null))
)
handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params))
} else {
tempNodes.push(nextNode)
}
}

for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
const pattern = node.#patterns[k]

const params = { ...node.#params }
const params = node.#params === emptyParams ? {} : { ...node.#params }

// Wildcard
// '/hello/*/foo' => match /hello/bar/foo
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))
tempNodes.push(astNode)
}
continue
Expand Down
Loading