From c85506b00ef9cd1bff492f63d8cbb5c6c6ad5dd9 Mon Sep 17 00:00:00 2001 From: lingyan Date: Thu, 14 May 2015 10:27:28 -0700 Subject: [PATCH] cache compiled result --- lib/router.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/router.js b/lib/router.js index a498834..b072f79 100644 --- a/lib/router.js +++ b/lib/router.js @@ -10,6 +10,7 @@ var pathToRegexp = require('path-to-regexp'); var METHODS = { GET: 'get' }; +var cachedCompilers = {}; /** * @class Route @@ -133,16 +134,17 @@ 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); @@ -150,7 +152,7 @@ Route.prototype.makePath = function (params) { 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);