Skip to content
Myr

perf(router): sort handlers by score only when necessary (#3697)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* perf(router): sort handlers by score only when necessary * add tests * Update node.test.ts

authored by EdamAmex · committed by GitHub · parent 8717946 · Browse files · 2 files+18 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−4 removed

  • tests:+11 added−0 removed
  • other:+7 added−4 removed

2 files+18 added−4 removed

modified:src/router/trie-router/node.test.ts+11 added
119 unmodified lines
120 expect(resB.length).toBe(1)121 expect(resB[0][0]).toEqual('resource')122 expect(resB[0][1]).toEqual({ id: 'b' })123 })124125 it('Should return a sorted values', () => {126 const node = new Node()127 node.insert('get', '/resource/a', 'A')128 node.insert('get', '/resource/*', 'Star')129 const [res] = node.search('get', '/resource/a')130 expect(res).not.toBeNull()131 expect(res.length).toBe(2)132 expect(res[0][0]).toEqual('A')133 expect(res[1][0]).toEqual('Star')134 })135})136137describe('Name path - Multiple route', () => {138 const node = new Node()
668 unmodified lines
modified:src/router/trie-router/node.ts+7 added-4 removed
196 unmodified lines
197 }198199 curNodes = tempNodes200 }201 const results = handlerSets.sort((a, b) => {202 return a.score - b.score203 })201202 if (handlerSets.length > 1) {203 handlerSets.sort((a, b) => {204 return a.score - b.score205 })206 }207205 return [results.map(({ handler, params }) => [handler, params] as [T, Params])]208 return [handlerSets.map(({ handler, params }) => [handler, params] as [T, Params])]209 }210}