Skip to content

Commit

Permalink
Merge pull request #26 from yahoo/cacheCompiled
Browse files Browse the repository at this point in the history
cache compiled result
  • Loading branch information
lingyan committed May 14, 2015
2 parents 97d14ff + c85506b commit 6969de6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var pathToRegexp = require('path-to-regexp');
var METHODS = {
GET: 'get'
};
var cachedCompilers = {};

/**
* @class Route
Expand Down Expand Up @@ -133,24 +134,25 @@ Route.prototype.match = function (url, options) {
* @for Route
*/
Route.prototype.makePath = function (params) {
var route = this.config.path,
compiler,
err;
var routePath = this.config.path;
var compiler;
var err;

if (Array.isArray(route)) {
route = route[0];
if (Array.isArray(routePath)) {
routePath = routePath[0];
}

if (typeof route === 'string') {
compiler = pathToRegexp.compile(route);
if (typeof routePath === 'string') {
compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath);
cachedCompilers[routePath] = compiler;

try {
return compiler(params);
} catch (e) {
err = e;
}
} else {
err = new TypeError('route must be a String path');
err = new TypeError('route path must be a string:' + routePath);
}

debug('Route.makePath failed, e = ', err);
Expand Down

0 comments on commit 6969de6

Please sign in to comment.