Skip to content

Commit

Permalink
Merge pull request #18 from delvedor/cache-find
Browse files Browse the repository at this point in the history
Call find only one time
  • Loading branch information
delvedor committed Jul 13, 2017
2 parents a963dda + 2133d0b commit f58fba5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 48 deletions.
60 changes: 25 additions & 35 deletions index.js
Expand Up @@ -154,6 +154,7 @@ Router.prototype.lookup = function (req, res) {
Router.prototype.find = function (method, path) {
var currentNode = this.tree
var node = null
var kind = 0
var decoded = null
var pindex = 0
var params = []
Expand All @@ -172,7 +173,7 @@ Router.prototype.find = function (method, path) {
// found the route
if (pathLen === 0 || path === prefix) {
var handle = currentNode.getHandler(method)
if (!handle) return null
if (handle === null) break

var paramNames = handle.params
var paramsObj = {}
Expand All @@ -192,62 +193,51 @@ Router.prototype.find = function (method, path) {
i = pathLen < prefixLen ? pathLen : prefixLen
while (len < i && path[len] === prefix[len]) len++

if (len === prefixLen) path = path.slice(len)
if (len === prefixLen) {
path = path.slice(len)
pathLen = path.length
}

node = currentNode.find(path[0])
if (!node) return null
kind = node.kind

// static route
node = currentNode.find(path[0], 0)
if (node) {
if (kind === 0) {
currentNode = node
continue
}

// parametric route
node = currentNode.findByKind(1)
if (node) {
} else if (kind === 1) { // parametric route
currentNode = node
i = 0
while (i < pathLen && path.charCodeAt(i) !== 47) i++
decoded = fastDecode(path.slice(0, i))
for (; i < pathLen && path.charCodeAt(i) !== 47; i++) {}
decoded = fastDecode(path.substring(0, i))
if (errored) {
return null
break
}
params[pindex++] = decoded
path = path.slice(i)
continue
}

// wildcard route
node = currentNode.findByKind(2)
if (node) {
path = path.substring(i)
} else if (kind === 2) { // wildcard route
decoded = fastDecode(path)
if (errored) {
return null
break
}
params[pindex] = decoded
currentNode = node
path = ''
continue
}

// parametric(regex) route
node = currentNode.findByKind(3)
if (node) {
} else if (kind === 3) { // parametric(regex) route
currentNode = node
i = 0
while (i < pathLen && path.charCodeAt(i) !== 47) i++
for (; i < pathLen && path.charCodeAt(i) !== 47; i++) {}
decoded = fastDecode(path.slice(0, i))
if (errored) {
return null
break
}
if (!node.regex.test(decoded)) return
if (!node.regex.test(decoded)) break
params[pindex++] = decoded
path = path.slice(i)
continue
}

// route not found
if (len !== prefixLen) return null
} else if (len !== prefixLen) break // route not found
}

return null
}

Router.prototype._defaultRoute = function (req, res) {
Expand Down
21 changes: 8 additions & 13 deletions node.js
Expand Up @@ -23,16 +23,6 @@ Node.prototype.add = function (node) {
this.numberOfChildren++
}

Node.prototype.find = function (label, kind) {
for (var i = 0; i < this.numberOfChildren; i++) {
var child = this.children[i]
if (child.label === label && child.kind === kind && (child.map || child.children.length)) {
return child
}
}
return null
}

Node.prototype.findByLabel = function (label) {
for (var i = 0; i < this.numberOfChildren; i++) {
var child = this.children[i]
Expand All @@ -43,11 +33,16 @@ Node.prototype.findByLabel = function (label) {
return null
}

Node.prototype.findByKind = function (kind) {
Node.prototype.find = function (label) {
for (var i = 0; i < this.numberOfChildren; i++) {
var child = this.children[i]
if (child.kind === kind && (child.map || child.children.length)) {
return child
if (child.map || child.children.length) {
if (child.label === label && child.kind === 0) {
return child
}
if (child.kind > 0) {
return child
}
}
}
return null
Expand Down

0 comments on commit f58fba5

Please sign in to comment.