Skip to content

Commit

Permalink
cache compiled result
Browse files Browse the repository at this point in the history
  • Loading branch information
lingyan committed May 14, 2015
1 parent 97d14ff commit c85506b
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/router.js
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 c85506b

Please sign in to comment.