Skip to content

Commit

Permalink
Added memoization for url parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkumardusad committed Mar 16, 2023
1 parent a9aa85a commit aef3cb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@routejs/router",
"version": "2.1.3",
"version": "2.1.4",
"description": "Fast and lightweight http routing engine for nodejs",
"main": "index.mjs",
"type": "module",
Expand Down
12 changes: 10 additions & 2 deletions src/router.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = class Router {
host: undefined,
};
#route = null;
#cache = {};

constructor(options = {}) {
if (options.caseSensitive === true) {
Expand Down Expand Up @@ -277,8 +278,15 @@ module.exports = class Router {
}

handle({ requestHost, requestMethod, requestUrl, request, response }) {
const parsedUrl = url.parse(requestUrl ? requestUrl : "");
const requestPath = decodeURIComponent(parsedUrl.pathname);
let requestPath;
if (this.#cache.hasOwnProperty(requestUrl)) {
requestPath = this.#cache[requestUrl];
} else {
const parsedUrl = url.parse(requestUrl ? requestUrl : "");
this.#cache[requestUrl] = decodeURI(parsedUrl.pathname);
requestPath = this.#cache[requestUrl];
}

const callStack = {
stack: this.routes(),
index: 0,
Expand Down
12 changes: 10 additions & 2 deletions src/router.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class Router {
host: undefined,
};
#route = null;
#cache = {};

constructor(options = {}) {
if (options.caseSensitive === true) {
Expand Down Expand Up @@ -277,8 +278,15 @@ export default class Router {
}

handle({ requestHost, requestMethod, requestUrl, request, response }) {
const parsedUrl = url.parse(requestUrl ? requestUrl : "");
const requestPath = decodeURIComponent(parsedUrl.pathname);
let requestPath;
if (this.#cache.hasOwnProperty(requestUrl)) {
requestPath = this.#cache[requestUrl];
} else {
const parsedUrl = url.parse(requestUrl ? requestUrl : "");
this.#cache[requestUrl] = decodeURI(parsedUrl.pathname);
requestPath = this.#cache[requestUrl];
}

const callStack = {
stack: this.routes(),
index: 0,
Expand Down

0 comments on commit aef3cb6

Please sign in to comment.