Skip to content
Myr

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

4 files+71 added−48 removed

modified:lib/router/index.js+20 added-18 removed
8 unmodified lines
9'use strict';1011/**12 * Module dependencies.13 * @private14 */1516var Route = require('./route');17var Layer = require('./layer');
5 unmodified lines
23var parseUrl = require('parseurl');2425/**26 * Module variables.27 * @private28 */2930var objectRegExp = /^\[object (\S+)\]$/;31var slice = Array.prototype.slice;
3 unmodified lines
35 * Initialize a new `Router` with the given `options`.36 *37 * @param {Object} options38 * @return {Router} which is an callable function37 * @api public39 * @public40 */4142var proto = module.exports = function(options) {41 options = options || {};43 var opts = options || {};4445 function router(req, res, next) {46 router.handle(req, res, next);47 }
2 unmodified lines
50 router.__proto__ = proto;5152 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 = [];5859 return router;60};
28 unmodified lines
89 *90 * @param {String} name91 * @param {Function} fn92 * @return {app} for chaining91 * @api public93 * @public94 */9596proto.param = function param(name, fn) {97 // param logic
30 unmodified lines
128};129130/**131 * Dispatch a req, res into the router.130 *131 * @api private132 * @private133 */134134proto.handle = function(req, res, done) {135proto.handle = function handle(req, res, out) {136 var self = this;137138 debug('dispatching %s %s', req.method, req.url);139
15 unmodified lines
155156 // 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');160161 // setup next layer162 req.next = next;163
151 unmodified lines
315};316317/**318 * Process any parameters for the layer.318 *319 * @api private319 * @private320 */321322proto.process_params = function(layer, called, req, res, done) {322proto.process_params = function process_params(layer, called, req, res, done) {323 var params = this.params;324325 // captured parameters from the layer, keys and values326 var keys = layer.keys;
94 unmodified lines
421 * 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 */427428proto.use = function use(fn) {429 var offset = 0;
20 unmodified lines
450 if (callbacks.length === 0) {451 throw new TypeError('Router.use() requires middleware functions');452 }453454 callbacks.forEach(function (fn) {454 for (var i = 0; i < callbacks.length; i++) {455 var fn = callbacks[i];456457 if (typeof fn !== 'function') {458 throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));459 }460
8 unmodified lines
469470 layer.route = undefined;471472 this.stack.push(layer);471 }, this);473 }474475 return this;476};477
6 unmodified lines
484 * and middleware to routes.485 *486 * @param {String} path487 * @return {Route}486 * @api public488 * @public489 */490489proto.route = function(path){491proto.route = function route(path) {492 var route = new Route(path);493494 var layer = new Layer(path, {495 sensitive: this.caseSensitive,
147 unmodified lines
modified:lib/router/layer.js+7 added-4 removed
modified:lib/router/route.js+43 added-26 removed
8 unmodified lines
9'use strict';1011/**12 * Module dependencies.13 * @private14 */1516var debug = require('debug')('express:router:route');17var flatten = require('array-flatten');18var Layer = require('./layer');19var methods = require('methods');2021/**21 * Expose `Route`.22 * Module variables.23 * @private24 */2526var slice = Array.prototype.slice;27var toString = Object.prototype.toString;2829/**30 * Module exports.31 * @public32 */3334module.exports = Route;3536/**37 * Initialize `Route` with the given `path`,38 *39 * @param {String} path30 * @api private40 * @public41 */4243function Route(path) {34 debug('new %s', path);44 this.path = path;45 this.stack = [];4647 debug('new %s', path);4849 // route handlers for various http methods50 this.methods = {};51}5253/**54 * Determine if the route handles a given method.43 * @api private55 * @private56 */5758Route.prototype._handles_method = function _handles_method(method) {59 if (this.methods._all) {60 return true;61 }6251 method = method.toLowerCase();63 var name = method.toLowerCase();6453 if (method === 'head' && !this.methods['head']) {54 method = 'get';65 if (name === 'head' && !this.methods['head']) {66 name = 'get';67 }6857 return Boolean(this.methods[method]);69 return Boolean(this.methods[name]);70};7172/**73 * @return {Array} supported HTTP methods62 * @api private74 * @private75 */7677Route.prototype._options = function _options() {78 var methods = Object.keys(this.methods);
12 unmodified lines
91};9293/**94 * dispatch req, res into this route83 *84 * @api private95 * @private96 */9787Route.prototype.dispatch = function(req, res, done){98Route.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 lines
160 * @return {Route} for chaining161 * @api public162 */163153Route.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);164Route.prototype.all = function all() {165 var handles = flatten(slice.call(arguments));166167 for (var i = 0; i < handles.length; i++) {168 var handle = handles[i];169170 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 }175162 var layer = Layer('/', {}, fn);176 var layer = Layer('/', {}, handle);177 layer.method = undefined;178179 this.methods._all = true;180 this.stack.push(layer);167 }, this);181 }182183 return this;184};185186methods.forEach(function(method){187 Route.prototype[method] = function(){174 var callbacks = flatten([].slice.call(arguments));188 var handles = flatten(slice.call(arguments));189176 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];192193 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 }198199 debug('%s %s', method, this.path);200185 var layer = Layer('/', {}, fn);201 var layer = Layer('/', {}, handle);202 layer.method = method;203204 this.methods[method] = true;205 this.stack.push(layer);190 }, this);206 }207208 return this;209 };210});
modified:History.md+1 added
71 unmodified lines
72 * 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