perf: Make ParamMap Array<[key, value]>. (#113)
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 68baca0 · Browse files · 3 files·+10 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+10 added−13 removed
- other:+10 added−13 removed
Provenance
- Commit
4c76a7d- Tree
2266cfc- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
68baca0
3 files changed+10 added−13 removed
modified:src/router/reg-exp-router/node.ts+2 added-4 removed
12345675678964 unmodified lines7475767780787980818226 unmodified linesconst LABEL_REG_EXP_STR = '[^/]+'const ONLY_WILDCARD_REG_EXP_STR = '.*'const TAIL_WILDCARD_REG_EXP_STR = '(?:|/.*)'
export interface ParamMap { [key: string]: number}export type ParamMap = Array<[string, number]>export interface Context { varIndex: number}
64 unmodified lines node.varIndex = context.varIndex++ } } if (name !== '') { paramMap[name] = node.varIndex paramMap.push([name, node.varIndex]) } } else { node = this.children[token] ||= new Node() }26 unmodified linesmodified:src/router/reg-exp-router/router.ts+7 added-8 removed
50 unmodified lines51 52 const index = match.indexOf('', 1)53 const [handler, paramMap] = handlers[replacementMap[index]]54 const params: { [key: string]: string } = {}55- const keys = Object.keys(paramMap)56- for (let i = 0; i < keys.length; i++) {57- params[keys[i]] = match[paramMap[keys[i]]]55+ for (let i = 0; i < paramMap.length; i++) {56+ params[paramMap[i][0]] = match[paramMap[i][1]]57 }58 return new Result(handler, params)59 }60 24 unmodified lines85 return86 }87 88 if (routes.length === 1 && routes[0][0] === '*') {90- this.matchers[method] = [true, null, [[routes[0][1], {}]]]89+ this.matchers[method] = [true, null, [[routes[0][1], []]]]90 return91 }92 93 if (routes.length === 1 && !routes[0][0].match(/:/)) {1 unmodified line95 const tmp = routes[0][0].endsWith('*')96 ? routes[0][0].replace(/\/\*$/, '(?:$|/)') // /path/to/* => /path/to(?:$|/)97 : `${routes[0][0]}$` // /path/to/action => /path/to/action$98 const regExpStr = `^${tmp.replace(/\*/g, '[^/]+')}` // /prefix/*/path/to => /prefix/[^/]+/path/to100- this.matchers[method] = [new RegExp(regExpStr), null, [[routes[0][1], {}]]]99+ this.matchers[method] = [new RegExp(regExpStr), null, [[routes[0][1], []]]]100 return101 }102 103 for (let i = 0; i < routes.length; i++) {3 unmodified lines107 108 const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp()109 for (let i = 0; i < handlers.length; i++) {110 const paramMap = handlers[i][1]112- Object.keys(paramMap).forEach((k) => {113- paramMap[k] = paramReplacementMap[paramMap[k]]114- })111+ for (let i = 0; i < paramMap.length; i++) {112+ paramMap[i][1] = paramReplacementMap[paramMap[i][1]]113+ }114 }115 116 this.matchers[method] = [new RegExp(regexp), indexReplacementMap, handlers]117 }1 unmodified linemodified:src/router/reg-exp-router/trie.ts+1 added-1 removed
7 unmodified lines89101112121314151631 unmodified lines7 unmodified lines context: Context = { varIndex: 0 } root: Node = new Node()
insert(path: string, index: number): ParamMap { const paramMap = {} const paramMap: ParamMap = []
/** * - pattern (:label, :label{0-9]+}, ...) * - /* wildcard31 unmodified lines