perf: remove argument reassignments in routing
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 5b4d4b4 · Browse files · 4 files·+71 added−48 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+71 added−48 removed
- other:+71 added−48 removed
Provenance
- Commit
9302acc- Tree
a344c33- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
5b4d4b4
4 files changed+71 added−48 removed
modified:lib/router/index.js+20 added-18 removed
8 unmodified lines9 'use strict';10 11 /**12 * Module dependencies.13+ * @private14 */15 16 var Route = require('./route');17 var Layer = require('./layer');5 unmodified lines23 var parseUrl = require('parseurl');24 25 /**26 * Module variables.27+ * @private28 */29 30 var objectRegExp = /^\[object (\S+)\]$/;31 var slice = Array.prototype.slice;3 unmodified lines35 * Initialize a new `Router` with the given `options`.36 *37 * @param {Object} options38 * @return {Router} which is an callable function37- * @api public39+ * @public40 */41 42 var proto = module.exports = function(options) {41- options = options || {};43+ var opts = options || {};44 45 function router(req, res, next) {46 router.handle(req, res, next);47 }2 unmodified lines50 router.__proto__ = proto;51 52 router.params = {};53 router._params = [];52- router.caseSensitive = options.caseSensitive;53- router.mergeParams = options.mergeParams;54- router.strict = options.strict;54+ router.caseSensitive = opts.caseSensitive;55+ router.mergeParams = opts.mergeParams;56+ router.strict = opts.strict;57 router.stack = [];58 59 return router;60 };28 unmodified lines89 *90 * @param {String} name91 * @param {Function} fn92 * @return {app} for chaining91- * @api public93+ * @public94 */95 96 proto.param = function param(name, fn) {97 // param logic30 unmodified lines128 };129 130 /**131 * Dispatch a req, res into the router.130- *131- * @api private132+ * @private133 */134 134-proto.handle = function(req, res, done) {135+proto.handle = function handle(req, res, out) {136 var self = this;137 138 debug('dispatching %s %s', req.method, req.url);139 15 unmodified lines155 156 // manage inter-router variables157 var parentParams = req.params;158 var parentUrl = req.baseUrl || '';158- done = restore(done, req, 'baseUrl', 'next', 'params');159+ var done = restore(out, req, 'baseUrl', 'next', 'params');160 161 // setup next layer162 req.next = next;163 151 unmodified lines315 };316 317 /**318 * Process any parameters for the layer.318- *319- * @api private319+ * @private320 */321 322-proto.process_params = function(layer, called, req, res, done) {322+proto.process_params = function process_params(layer, called, req, res, done) {323 var params = this.params;324 325 // captured parameters from the layer, keys and values326 var keys = layer.keys;94 unmodified lines421 * to the handler function. The main effect of this feature is that mounted422 * handlers can operate without any code changes regardless of the "prefix"423 * pathname.424 *425- * @api public425+ * @public426 */427 428 proto.use = function use(fn) {429 var offset = 0;20 unmodified lines450 if (callbacks.length === 0) {451 throw new TypeError('Router.use() requires middleware functions');452 }453 454- callbacks.forEach(function (fn) {454+ for (var i = 0; i < callbacks.length; i++) {455+ var fn = callbacks[i];456+457 if (typeof fn !== 'function') {458 throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));459 }460 8 unmodified lines469 470 layer.route = undefined;471 472 this.stack.push(layer);471- }, this);473+ }474 475 return this;476 };477 6 unmodified lines484 * and middleware to routes.485 *486 * @param {String} path487 * @return {Route}486- * @api public488+ * @public489 */490 489-proto.route = function(path){491+proto.route = function route(path) {492 var route = new Route(path);493 494 var layer = new Layer(path, {495 sensitive: this.caseSensitive,147 unmodified linesmodified:lib/router/layer.js+7 added-4 removed
8 unmodified lines91011121314151617181920212223242526252728293031322 unmodified lines3536373836394041424344424546444748495051125 unmodified linesExpand all8 unmodified lines'use strict';
/** * Module dependencies. * @private */
var pathRegexp = require('path-to-regexp');var debug = require('debug')('express:router:layer');
/** * Module variables. * @private */
var hasOwnProperty = Object.prototype.hasOwnProperty;
/** * Expose `Layer`. * Module exports. * @public */
module.exports = Layer;
2 unmodified lines return new Layer(path, options, fn); }
debug('new %s', path); options = options || {}; var opts = options || {};
this.handle = fn; this.name = fn.name || '<anonymous>'; this.params = undefined; this.path = undefined; this.regexp = pathRegexp(path, this.keys = [], options); this.regexp = pathRegexp(path, this.keys = [], opts);
if (path === '/' && options.end === false) { if (path === '/' && opts.end === false) { this.regexp.fast_slash = true; }}
125 unmodified linesExpand allmodified:lib/router/route.js+43 added-26 removed
8 unmodified lines9 'use strict';10 11 /**12 * Module dependencies.13+ * @private14 */15 16 var debug = require('debug')('express:router:route');17 var flatten = require('array-flatten');18 var Layer = require('./layer');19 var methods = require('methods');20 21 /**21- * Expose `Route`.22+ * Module variables.23+ * @private24 */25 26+var slice = Array.prototype.slice;27+var toString = Object.prototype.toString;28+29+/**30+ * Module exports.31+ * @public32+ */33+34 module.exports = Route;35 36 /**37 * Initialize `Route` with the given `path`,38 *39 * @param {String} path30- * @api private40+ * @public41 */42 43 function Route(path) {34- debug('new %s', path);44 this.path = path;45 this.stack = [];46 47+ debug('new %s', path);48+49 // route handlers for various http methods50 this.methods = {};51 }52 53 /**54+ * Determine if the route handles a given method.43- * @api private55+ * @private56 */57 58 Route.prototype._handles_method = function _handles_method(method) {59 if (this.methods._all) {60 return true;61 }62 51- method = method.toLowerCase();63+ var name = method.toLowerCase();64 53- if (method === 'head' && !this.methods['head']) {54- method = 'get';65+ if (name === 'head' && !this.methods['head']) {66+ name = 'get';67 }68 57- return Boolean(this.methods[method]);69+ return Boolean(this.methods[name]);70 };71 72 /**73 * @return {Array} supported HTTP methods62- * @api private74+ * @private75 */76 77 Route.prototype._options = function _options() {78 var methods = Object.keys(this.methods);12 unmodified lines91 };92 93 /**94 * dispatch req, res into this route83- *84- * @api private95+ * @private96 */97 87-Route.prototype.dispatch = function(req, res, done){98+Route.prototype.dispatch = function dispatch(req, res, done) {99 var idx = 0;100 var stack = this.stack;101 if (stack.length === 0) {102 return done();57 unmodified lines160 * @return {Route} for chaining161 * @api public162 */163 153-Route.prototype.all = function(){154- var callbacks = flatten([].slice.call(arguments));155- callbacks.forEach(function(fn) {156- if (typeof fn !== 'function') {157- var type = {}.toString.call(fn);164+Route.prototype.all = function all() {165+ var handles = flatten(slice.call(arguments));166+167+ for (var i = 0; i < handles.length; i++) {168+ var handle = handles[i];169+170+ if (typeof handle !== 'function') {171+ var type = toString.call(handle);172 var msg = 'Route.all() requires callback functions but got a ' + type;159- throw new Error(msg);173+ throw new TypeError(msg);174 }175 162- var layer = Layer('/', {}, fn);176+ var layer = Layer('/', {}, handle);177 layer.method = undefined;178 179 this.methods._all = true;180 this.stack.push(layer);167- }, this);181+ }182 183 return this;184 };185 186 methods.forEach(function(method){187 Route.prototype[method] = function(){174- var callbacks = flatten([].slice.call(arguments));188+ var handles = flatten(slice.call(arguments));189 176- callbacks.forEach(function(fn) {177- if (typeof fn !== 'function') {178- var type = {}.toString.call(fn);190+ for (var i = 0; i < handles.length; i++) {191+ var handle = handles[i];192+193+ if (typeof handle !== 'function') {194+ var type = toString.call(handle);195 var msg = 'Route.' + method + '() requires callback functions but got a ' + type;196 throw new Error(msg);197 }198 199 debug('%s %s', method, this.path);200 185- var layer = Layer('/', {}, fn);201+ var layer = Layer('/', {}, handle);202 layer.method = method;203 204 this.methods[method] = true;205 this.stack.push(layer);190- }, this);206+ }207+208 return this;209 };210 });modified:History.md+1 added
71 unmodified lines72 * perf: isolate `app.render` try block73 * perf: remove argument reassignments in application74 * perf: remove argument reassignments in request prototype75 * perf: remove argument reassignments in response prototype76+ * perf: remove argument reassignments in routing77 * perf: remove argument reassignments in `View`78 * perf: skip attempting to decode zero length string79 * perf: use saved reference to `http.STATUS_CODES`80 2883 unmodified lines