Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache compiled result #26

Merged
merged 1 commit into from
May 14, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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