Skip to content
Myr

perf(util): use indexOf instead of RegExp.match for extracting URL. (#293)

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

authored by Taku Amano · committed by GitHub · parent 9da10ac · Browse files · 3 files+9 added−16 removed
This commit

State

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

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

3 files+9 added−16 removed

  • tests:+2 added−4 removed
  • other:+7 added−12 removed

3 files+9 added−16 removed

modified:src/utils/url.test.ts+2 added-4 removed
modified:src/utils/url.ts+6 added-11 removed
35 unmodified lines
3637 return null38}3940type Params = {41 strict: boolean42}40export const getPathFromURL = (url: string, strict: boolean = true): string => {41 const queryIndex = url.indexOf('?')42 const result = url.substring(url.indexOf('/', 8), queryIndex === -1 ? url.length : queryIndex)4344export const getPathFromURL = (url: string, params: Params = { strict: true }): string => {44 // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same45 // default is true47 if (params.strict === false && url.endsWith('/')) {48 url = url.slice(0, -1)46 if (strict === false && result.endsWith('/')) {47 return result.slice(0, -1)48 }4951 const match = url.match(URL_REGEXP)52 if (match) {53 return match[1]54 }55 return ''50 return result51}5253export const isAbsoluteURL = (url: string): boolean => {54 const match = url.match(URL_REGEXP)
37 unmodified lines
modified:src/hono.ts+1 added-1 removed
155 unmodified lines
156 return this.router.match(method, path)157 }158159 private async dispatch(request: Request, event?: FetchEvent, env?: E): Promise<Response> {160 const path = getPathFromURL(request.url, { strict: this.strict })160 const path = getPathFromURL(request.url, this.strict)161 const method = request.method162163 const result = this.matchRoute(method, path)164
39 unmodified lines