Skip to content
Myr

perf: remove argument reassignments in application

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 a8a8564 · Browse files · 2 files+101 added−74 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+101 added−74 removed

  • other:+101 added−74 removed

2 files+101 added−74 removed

modified:lib/application.js+100 added-74 removed
8 unmodified lines
9'use strict';1011/**12 * Module dependencies.13 * @api private13 * @private14 */1516var finalhandler = require('finalhandler');17var Router = require('./router');
19 unmodified lines
37var app = exports = module.exports = {};3839/**40 * Variable for trust proxy inheritance back-compat41 * @api private41 * @private42 */4344var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';45
3 unmodified lines
49 * - setup default configuration50 * - setup default middleware51 * - setup route reflection methods52 *53 * @api private53 * @private54 */5556app.init = function(){56app.init = function init() {57 this.cache = {};58 this.settings = {};58 this.engines = {};59 this.settings = {};6061 this.defaultConfiguration();62};6364/**65 * Initialize application configuration.65 *66 * @api private66 * @private67 */6869app.defaultConfiguration = function(){69app.defaultConfiguration = function defaultConfiguration() {70 var env = process.env.NODE_ENV || 'development';7172 // default settings73 this.enable('x-powered-by');74 this.set('etag', 'weak');73 var env = process.env.NODE_ENV || 'development';75 this.set('env', env);76 this.set('query parser', 'extended');77 this.set('subdomain offset', 2);78 this.set('trust proxy', false);
51 unmodified lines
130 *131 * We cannot add the base router in the defaultConfiguration because132 * it reads app settings which might be set after that has run.133 *133 * @api private134 * @private135 */135app.lazyrouter = function() {136app.lazyrouter = function lazyrouter() {137 if (!this._router) {138 this._router = new Router({139 caseSensitive: this.enabled('case sensitive routing'),140 strict: this.enabled('strict routing')
6 unmodified lines
147148/**149 * Dispatch a req, res pair into the application. Starts pipeline processing.150 *150 * If no _done_ callback is provided, then default error handlers will respond151 * If no callback is provided, then default error handlers will respond152 * in the event of an error bubbling through the stack.153 *153 * @api private154 * @private155 */156156app.handle = function(req, res, done) {157app.handle = function handle(req, res, callback) {158 var router = this._router;159160 // final handler160 done = done || finalhandler(req, res, {161 var done = callback || finalhandler(req, res, {162 env: this.get('env'),163 onerror: logerror.bind(this)164 });165
13 unmodified lines
179 *180 * If the _fn_ parameter is an express app, then it will be181 * mounted at the _route_ specified.182 *182 * @api public183 * @public184 */185186app.use = function use(fn) {187 var offset = 0;
58 unmodified lines
246 *247 * Routes are isolated middleware stacks for specific paths.248 * See the Route api docs for details.249 *249 * @api public250 * @public251 */252252app.route = function(path){253app.route = function route(path) {254 this.lazyrouter();255 return this._router.route(path);256};257
27 unmodified lines
285 *286 * @param {String} ext287 * @param {Function} fn288 * @return {app} for chaining288 * @api public289 * @public290 */291291app.engine = function(ext, fn){292 if ('function' != typeof fn) throw new Error('callback function required');293 if ('.' != ext[0]) ext = '.' + ext;294 this.engines[ext] = fn;292app.engine = function engine(ext, fn) {293 if (typeof fn !== 'function') {294 throw new Error('callback function required');295 }296297 // get file extension298 var extension = ext[0] !== '.'299 ? '.' + ext300 : ext;301302 // store engine303 this.engines[extension] = fn;304305 return this;306};307308/**
4 unmodified lines
313 *314 * @param {String|Array} name315 * @param {Function} fn316 * @return {app} for chaining307 * @api public317 * @public318 */319310app.param = function(name, fn){320app.param = function param(name, fn) {321 this.lazyrouter();322323 if (Array.isArray(name)) {314 name.forEach(function(key) {315 this.param(key, fn);316 }, this);324 for (var i = 0; i < name.length; i++) {325 this.param(name[i], fn);326 }327328 return this;329 }330331 this._router.param(name, fn);332333 return this;334};335336/**
7 unmodified lines
344 *345 * @param {String} setting346 * @param {*} [val]347 * @return {Server} for chaining336 * @api public348 * @public349 */350339app.set = function(setting, val){351app.set = function set(setting, val) {352 if (arguments.length === 1) {353 // app.get(setting)354 return this.settings[setting];355 }
37 unmodified lines
393 * was mounted as "/blog" then the394 * return value would be "/blog/admin".395 *396 * @return {String}385 * @api private397 * @private398 */399388app.path = function(){400app.path = function path() {401 return this.parent402 ? this.parent.path() + this.mountpath403 : '';404};
9 unmodified lines
414 * // => true415 *416 * @param {String} setting417 * @return {Boolean}406 * @api public418 * @public419 */420409app.enabled = function(setting){410 return !!this.set(setting);421app.enabled = function enabled(setting) {422 return Boolean(this.set(setting));423};424425/**426 * Check if `setting` is disabled.
6 unmodified lines
433 * // => false434 *435 * @param {String} setting436 * @return {Boolean}425 * @api public437 * @public438 */439428app.disabled = function(setting){440app.disabled = function disabled(setting) {441 return !this.set(setting);442};443444/**445 * Enable `setting`.446 *447 * @param {String} setting448 * @return {app} for chaining437 * @api public449 * @public450 */451440app.enable = function(setting){452app.enable = function enable(setting) {453 return this.set(setting, true);454};455456/**457 * Disable `setting`.458 *459 * @param {String} setting460 * @return {app} for chaining449 * @api public461 * @public462 */463452app.disable = function(setting){464app.disable = function disable(setting) {465 return this.set(setting, false);466};467468/**
1 unmodified line
470 */471472methods.forEach(function(method){473 app[method] = function(path){462 if ('get' == method && 1 == arguments.length) return this.set(path);474 if (method === 'get' && arguments.length === 1) {475 // app.get(setting)476 return this.set(path);477 }478479 this.lazyrouter();480481 var route = this._router.route(path);
8 unmodified lines
490 *491 * @param {String} path492 * @param {Function} ...493 * @return {app} for chaining479 * @api public494 * @public495 */496482app.all = function(path){497app.all = function all(path) {498 this.lazyrouter();499500 var route = this._router.route(path);501 var args = slice.call(arguments, 1);487 methods.forEach(function(method){488 route[method].apply(route, args);489 });502503 for (var i = 0; i < methods.length; i++) {504 route[methods[i]].apply(route, args);505 }506507 return this;508};509510// del -> delete alias
12 unmodified lines
523 * })524 *525 * @param {String} name526 * @param {String|Function} options or fn511 * @param {Function} fn512 * @api public527 * @param {Function} callback528 * @public529 */530515app.render = function(name, options, fn){516 var opts = {};531app.render = function render(name, options, callback) {532 var cache = this.cache;533 var done = callback;534 var engines = this.engines;535 var opts = options;536 var renderOptions = {};537 var view;538539 // support callback function as second arg522 if ('function' == typeof options) {523 fn = options, options = {};540 if (typeof options === 'function') {541 done = options;542 opts = {};543 }544545 // merge app.locals527 merge(opts, this.locals);546 merge(renderOptions, this.locals);547548 // merge options._locals530 if (options._locals) {531 merge(opts, options._locals);549 if (opts._locals) {550 merge(renderOptions, opts._locals);551 }552553 // merge options535 merge(opts, options);554 merge(renderOptions, opts);555556 // set .cache unless explicitly provided538 opts.cache = null == opts.cache539 ? this.enabled('view cache')540 : opts.cache;557 if (renderOptions.cache == null) {558 renderOptions.cache = this.enabled('view cache');559 }560561 // primed cache543 if (opts.cache) view = cache[name];562 if (renderOptions.cache) {563 view = cache[name];564 }565566 // view567 if (!view) {547 view = new (this.get('view'))(name, {568 var View = this.get('view');569570 view = new View(name, {571 defaultEngine: this.get('view engine'),572 root: this.get('views'),573 engines: engines574 });
3 unmodified lines
578 ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'579 : 'directory "' + view.root + '"'580 var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);581 err.view = view;559 return fn(err);582 return done(err);583 }584585 // prime the cache563 if (opts.cache) cache[name] = view;586 if (renderOptions.cache) {587 cache[name] = view;588 }589 }590591 // render592 try {568 view.render(opts, fn);593 view.render(renderOptions, done);594 } catch (err) {570 fn(err);595 done(err);596 }597};598599/**
13 unmodified lines
613 * http.createServer(app).listen(80);614 * https.createServer({ ... }, app).listen(443);615 *616 * @return {http.Server}592 * @api public617 * @public618 */619595app.listen = function(){620app.listen = function listen() {621 var server = http.createServer(this);622 return server.listen.apply(server, arguments);623};624625/**601* Log error using console.error.602*603* @param {Error} err604* @api private605*/626 * Log error using console.error.627 *628 * @param {Error} err629 * @private630 */631607function logerror(err){632function logerror(err) {633 /* istanbul ignore next */634 if (this.get('env') !== 'test') console.error(err.stack || err.toString());635}
modified:History.md+1 added
64 unmodified lines
65 - deps: mime-types@~2.1.166 - perf: reduce try block size67 - perf: remove bitwise operations68 * perf: enable strict mode69 * perf: remove argument reassignments in application70 * perf: remove argument reassignments in `View`71 * perf: use saved reference to `http.STATUS_CODES`72734.12.4 / 2015-05-17
2882 unmodified lines