Skip to content

Commit 6a15092

Browse files
committed
Optimize path function by pre-compiling regex to avoid recompilation overhead.
1 parent 29150df commit 6a15092

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,13 @@ export function subst(template: string, params: ParamMap): string {
154154
return renderedPath
155155
}
156156

157+
// Pre-compile regex for better performance - avoids recompilation overhead on each call
158+
const PATH_PARAM_REGEX = /:[_A-Za-z]+\w*/g
159+
157160
function path(template: string, params: ParamMap) {
158161
const remainingParams = { ...params }
159162

160-
const renderedPath = template.replace(/:[_A-Za-z]+\w*/g, (p) => {
163+
const renderedPath = template.replace(PATH_PARAM_REGEX, (p) => {
161164
const key = p.slice(1)
162165

163166
validatePathParam(params, key)

0 commit comments

Comments
 (0)