perf(trie-router): fine tuning, 9~10% faster (#473)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* perf(trie-router): fine tuning, 9~10% faster! * update readme
authored by Yusuke Wada · committed by GitHub · parent 869ab52 · Browse files · 4 files·+36 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+36 added−30 removed
- other:+36 added−30 removed
Provenance
- Commit
83a95ba- Tree
5c146ff- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
869ab52
4 files changed+36 added−30 removed
modified:deno_dist/router/trie-router/node.ts+12 added-9 removed
107 unmodified lines108 109 private getHandlerSets(node: Node<T>, method: string, wildcard?: boolean): HandlerSet<T>[] {110 return (node.handlerSetCache[`${method}:${wildcard ? '1' : '0'}`] ||= (() => {111 const handlerSets: HandlerSet<T>[] = []112- node.methods.map((m) => {112+ for (let i = 0, len = node.methods.length; i < len; i++) {113+ const m = node.methods[i]114 const handlerSet = m[method] || m[METHOD_NAME_ALL]115 if (handlerSet !== undefined) {115- const hs = { ...handlerSet }116- handlerSets.push(hs)116+ handlerSets.push(handlerSet)117- return117 }119- })120-118+ }119 return handlerSets120 })())121 }122 23 unmodified lines146 handlerSets.push(...this.getHandlerSets(nextNode.children['*'], method, true))147 }148 handlerSets.push(...this.getHandlerSets(nextNode, method))149 matched = true150+ } else {151+ tempNodes.push(nextNode)152 }153- tempNodes.push(nextNode)153 }154 155 for (let k = 0, len3 = node.patterns.length; k < len3; k++) {156 const pattern = node.patterns[k]17 unmodified lines174 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {175 if (typeof key === 'string') {176 if (isLast === true) {177 handlerSets.push(...this.getHandlerSets(node.children[key], method))178+ } else {179+ tempNodes.push(node.children[key])180 }180- tempNodes.push(node.children[key])181 }182 183 // '/book/a' => not-slug184 // '/book/:slug' => slug1 unmodified line186 // GET /book/foo ~> slug, param['slug'] => foo187 if (typeof name === 'string' && !matched) {188 params[name] = part189 }190+ break191 }192 }193 }194 195 curNodes = tempNodes196 }197 197- if (handlerSets.length <= 0) return null198+ const len = handlerSets.length199+ if (len === 0) return null200+ if (len === 1) return { handlers: [handlerSets[0].handler], params }201 202 const handlers = handlerSets203 .sort((a, b) => {204 return a.score - b.score8 unmodified linesmodified:deno_dist/README.md+6 added-6 removed
44 unmodified lines45464748495051525349505152535455555657585929 unmodified lines44 unmodified lines
**Hono is fastest**, compared to other routers for Cloudflare Workers.
```plainhono - trie-router(default) x 424,449 ops/sec ±4.98% (77 runs sampled)hono - regexp-router x 516,228 ops/sec ±4.79% (81 runs sampled)itty-router x 206,641 ops/sec ±3.59% (87 runs sampled)sunder x 319,500 ops/sec ±1.33% (93 runs sampled)worktop x 187,280 ops/sec ±3.09% (87 runs sampled)hono - trie-router(default) x 437,354 ops/sec ±4.58% (83 runs sampled)hono - regexp-router x 535,157 ops/sec ±3.46% (83 runs sampled)itty-router x 203,092 ops/sec ±3.64% (89 runs sampled)sunder x 308,252 ops/sec ±2.17% (91 runs sampled)worktop x 190,764 ops/sec ±3.11% (86 runs sampled)Fastest is hono - regexp-router✨ Done in 38.32s.✨ Done in 38.26s.```
## Documentation
29 unmodified linesmodified:src/router/trie-router/node.ts+12 added-9 removed
107 unmodified lines108 109 private getHandlerSets(node: Node<T>, method: string, wildcard?: boolean): HandlerSet<T>[] {110 return (node.handlerSetCache[`${method}:${wildcard ? '1' : '0'}`] ||= (() => {111 const handlerSets: HandlerSet<T>[] = []112- node.methods.map((m) => {112+ for (let i = 0, len = node.methods.length; i < len; i++) {113+ const m = node.methods[i]114 const handlerSet = m[method] || m[METHOD_NAME_ALL]115 if (handlerSet !== undefined) {115- const hs = { ...handlerSet }116- handlerSets.push(hs)116+ handlerSets.push(handlerSet)117- return117 }119- })120-118+ }119 return handlerSets120 })())121 }122 23 unmodified lines146 handlerSets.push(...this.getHandlerSets(nextNode.children['*'], method, true))147 }148 handlerSets.push(...this.getHandlerSets(nextNode, method))149 matched = true150+ } else {151+ tempNodes.push(nextNode)152 }153- tempNodes.push(nextNode)153 }154 155 for (let k = 0, len3 = node.patterns.length; k < len3; k++) {156 const pattern = node.patterns[k]17 unmodified lines174 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {175 if (typeof key === 'string') {176 if (isLast === true) {177 handlerSets.push(...this.getHandlerSets(node.children[key], method))178+ } else {179+ tempNodes.push(node.children[key])180 }180- tempNodes.push(node.children[key])181 }182 183 // '/book/a' => not-slug184 // '/book/:slug' => slug1 unmodified line186 // GET /book/foo ~> slug, param['slug'] => foo187 if (typeof name === 'string' && !matched) {188 params[name] = part189 }190+ break191 }192 }193 }194 195 curNodes = tempNodes196 }197 197- if (handlerSets.length <= 0) return null198+ const len = handlerSets.length199+ if (len === 0) return null200+ if (len === 1) return { handlers: [handlerSets[0].handler], params }201 202 const handlers = handlerSets203 .sort((a, b) => {204 return a.score - b.score8 unmodified linesmodified:README.md+6 added-6 removed
44 unmodified lines45464748495051525349505152535455555657585929 unmodified lines44 unmodified lines
**Hono is fastest**, compared to other routers for Cloudflare Workers.
```plainhono - trie-router(default) x 424,449 ops/sec ±4.98% (77 runs sampled)hono - regexp-router x 516,228 ops/sec ±4.79% (81 runs sampled)itty-router x 206,641 ops/sec ±3.59% (87 runs sampled)sunder x 319,500 ops/sec ±1.33% (93 runs sampled)worktop x 187,280 ops/sec ±3.09% (87 runs sampled)hono - trie-router(default) x 437,354 ops/sec ±4.58% (83 runs sampled)hono - regexp-router x 535,157 ops/sec ±3.46% (83 runs sampled)itty-router x 203,092 ops/sec ±3.64% (89 runs sampled)sunder x 308,252 ops/sec ±2.17% (91 runs sampled)worktop x 190,764 ops/sec ±3.11% (86 runs sampled)Fastest is hono - regexp-router✨ Done in 38.32s.✨ Done in 38.26s.```
## Documentation
29 unmodified lines