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 842ecdd
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 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,
var routePath = this.config.path,
compiler,
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');
}

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

0 comments on commit 842ecdd

Please sign in to comment.