From aaa7cd217c6707c605e3b2fb29bb707b1f687230 Mon Sep 17 00:00:00 2001 From: Nigro Simone Date: Sun, 17 Aug 2025 09:56:15 +0200 Subject: [PATCH] perf: typeof will be faster --- src/router.js | 2 +- src/utils.js | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/router.js b/src/router.js index 37e53d18..d8aae36f 100644 --- a/src/router.js +++ b/src/router.js @@ -153,7 +153,7 @@ module.exports = class Router extends EventEmitter { all: method === 'ALL' || method === 'USE', gettable: method === 'GET' || method === 'HEAD', }; - if(typeof route.path === 'string' && (route.path.includes(':') || route.path.includes('*') || (route.path.includes('(') && route.path.includes(')'))) && route.pattern instanceof RegExp) { + if(typeof route.path === 'string' && (route.path.includes(':') || route.path.includes('*') || (route.path.includes('(') && route.path.includes(')'))) && typeof route.pattern === 'object') { route.complex = true; } routes.push(route); diff --git a/src/utils.js b/src/utils.js index 7d6eba09..73cb98c4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -44,7 +44,7 @@ function removeDuplicateSlashes(path) { } function patternToRegex(pattern, isPrefix = false) { - if(pattern instanceof RegExp) { + if(typeof pattern === 'object') { return pattern; } if(isPrefix && pattern === '') { @@ -64,19 +64,18 @@ function patternToRegex(pattern, isPrefix = false) { } function needsConversionToRegex(pattern) { - if(pattern instanceof RegExp) { + if(pattern === '/*') { return false; } - if(pattern === '/*') { + if(typeof pattern === 'object') { return false; } - - return pattern.includes('*') || + return pattern.includes(':') || + pattern.includes('*') || pattern.includes('?') || pattern.includes('+') || pattern.includes('(') || pattern.includes(')') || - pattern.includes(':') || pattern.includes('{') || pattern.includes('}') || pattern.includes('[') || @@ -87,7 +86,7 @@ function canBeOptimized(pattern) { if(pattern === '/*') { return false; } - if(pattern instanceof RegExp) { + if(typeof pattern === 'object') { return false; } if(