Skip to content

Commit 9c669c8

Browse files
committed
introducing router matching cache
1 parent 541e040 commit 9c669c8

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/router/sequential.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const STATUS_500 = {
1010
}
1111

1212
module.exports = (config = {}) => {
13+
const cache = new Map()
14+
1315
if (!config.defaultRoute) {
1416
config.defaultRoute = () => {
1517
return new Response(null, STATUS_404)
@@ -45,8 +47,16 @@ module.exports = (config = {}) => {
4547
req.path = path || '/'
4648
req.query = queryIndex > 0 ? qs.parse(url.substring(queryIndex + 1)) : {}
4749

48-
const match = router.find(req.method, req.path)
49-
if (match.handlers.length > 0) {
50+
const cacheKey = `${req.method}:${req.path}`
51+
let match = null
52+
if (cache.has(cacheKey)) {
53+
match = cache.get(cacheKey)
54+
} else {
55+
match = router.find(req.method, req.path)
56+
cache.set(cacheKey, match)
57+
}
58+
59+
if (match?.handlers?.length > 0) {
5060
if (!req.params) {
5161
req.params = {}
5262
}

0 commit comments

Comments
 (0)