Skip to content

Commit 081b811

Browse files
committed
perf: add fast match path for "*" route
1 parent 1f71fae commit 081b811

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ unreleased
6262
- Use `res.getHeaderNames()` when available
6363
- Use `res.headersSent` when available
6464
65+
* perf: add fast match path for `*` route
6566
* perf: improve `req.ips` performance
6667

6768
4.14.1 / 2017-01-28

lib/router/layer.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ function Layer(path, options, fn) {
4444
this.path = undefined;
4545
this.regexp = pathRegexp(path, this.keys = [], opts);
4646

47-
if (path === '/' && opts.end === false) {
48-
this.regexp.fast_slash = true;
49-
}
47+
// set fast path flags
48+
this.regexp.fast_star = path === '*'
49+
this.regexp.fast_slash = path === '/' && opts.end === false
5050
}
5151

5252
/**
@@ -111,13 +111,20 @@ Layer.prototype.match = function match(path) {
111111
var match
112112

113113
if (path != null) {
114+
// fast path non-ending match for / (any path matches)
114115
if (this.regexp.fast_slash) {
115-
// fast path non-ending match for / (everything matches)
116116
this.params = {}
117117
this.path = ''
118118
return true
119119
}
120120

121+
// fast path for * (everything matched in a param)
122+
if (this.regexp.fast_star) {
123+
this.params = {'0': decode_param(path)}
124+
this.path = path
125+
return true
126+
}
127+
121128
// match the path
122129
match = this.regexp.exec(path)
123130
}

0 commit comments

Comments
 (0)