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(router): sort handlers by score only when necessary #3697

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions src/router/trie-router/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ describe('Name path', () => {
expect(resB[0][0]).toEqual('resource')
expect(resB[0][1]).toEqual({ id: 'b' })
})

it('Should return a sorted values', () => {
const node = new Node()
node.insert('get', '/resource/a', 'A')
node.insert('get', '/resource/*', 'Star')
const [res] = node.search('get', '/resource/a')
expect(res).not.toBeNull()
expect(res.length).toBe(2)
expect(res[0][0]).toEqual('A')
expect(res[1][0]).toEqual('Star')
})
})

describe('Name path - Multiple route', () => {
Expand Down
11 changes: 7 additions & 4 deletions src/router/trie-router/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@

curNodes = tempNodes
}
const results = handlerSets.sort((a, b) => {
return a.score - b.score
})

return [results.map(({ handler, params }) => [handler, params] as [T, Params])]
if (handlerSets.length > 1) {
handlerSets.sort((a, b) => {
return a.score - b.score
})
}

Check warning on line 207 in src/router/trie-router/node.ts

View check run for this annotation

Codecov / codecov/patch

src/router/trie-router/node.ts#L207

Added line #L207 was not covered by tests
EdamAme-x marked this conversation as resolved.
Show resolved Hide resolved
return [handlerSets.map(({ handler, params }) => [handler, params] as [T, Params])]
}
}
Loading