Skip to content

Commit

Permalink
Merge pull request #7 from billouboq/performance
Browse files Browse the repository at this point in the history
optimize lookup
  • Loading branch information
delvedor authored May 20, 2017
2 parents 8d5ab52 + 33048b2 commit 299b4c0
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ Router.prototype._insert = function (method, path, kind, params, handler, store)
}

Router.prototype.lookup = function (req, res) {
var i = 0
while (i < req.url.length && req.url.charCodeAt(i) !== 63 /* ? */ && req.url.charCodeAt(i) !== 35 /* # */) i++
var handle = this.find(req.method, req.url.slice(0, i))
var handle = this.find(req.method, sanitizeUrl(req.url))
if (!handle) return this._defaultRoute(req, res)
return handle.handler(req, res, handle.params, handle.store)
}
Expand Down Expand Up @@ -231,3 +229,13 @@ Router.prototype._defaultRoute = function (req, res) {
}

module.exports = Router

function sanitizeUrl (url) {
for (var i = 0; i < url.length; i++) {
var charCode = url.charCodeAt(i)
if (charCode === 63 /* ? */ || charCode === 35 /* # */) {
return url.slice(0, i)
}
}
return url
}

0 comments on commit 299b4c0

Please sign in to comment.