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
Provenance
- Commit
1050248- Tree
6db2480- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
a8a8564
2 files changed+101 added−74 removed
modified:lib/application.js+100 added-74 removed
8 unmodified lines9 'use strict';10 11 /**12 * Module dependencies.13- * @api private13+ * @private14 */15 16 var finalhandler = require('finalhandler');17 var Router = require('./router');19 unmodified lines37 var app = exports = module.exports = {};38 39 /**40 * Variable for trust proxy inheritance back-compat41- * @api private41+ * @private42 */43 44 var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';45 3 unmodified lines49 * - setup default configuration50 * - setup default middleware51 * - setup route reflection methods52 *53- * @api private53+ * @private54 */55 56-app.init = function(){56+app.init = function init() {57 this.cache = {};58- this.settings = {};58 this.engines = {};59+ this.settings = {};60+61 this.defaultConfiguration();62 };63 64 /**65 * Initialize application configuration.65- *66- * @api private66+ * @private67 */68 69-app.defaultConfiguration = function(){69+app.defaultConfiguration = function defaultConfiguration() {70+ var env = process.env.NODE_ENV || 'development';71+72 // 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 lines130 *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 */135-app.lazyrouter = function() {136+app.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 lines147 148 /**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 */156 156-app.handle = function(req, res, done) {157+app.handle = function handle(req, res, callback) {158 var router = this._router;159 160 // 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 lines179 *180 * If the _fn_ parameter is an express app, then it will be181 * mounted at the _route_ specified.182 *182- * @api public183+ * @public184 */185 186 app.use = function use(fn) {187 var offset = 0;58 unmodified lines246 *247 * Routes are isolated middleware stacks for specific paths.248 * See the Route api docs for details.249 *249- * @api public250+ * @public251 */252 252-app.route = function(path){253+app.route = function route(path) {254 this.lazyrouter();255 return this._router.route(path);256 };257 27 unmodified lines285 *286 * @param {String} ext287 * @param {Function} fn288 * @return {app} for chaining288- * @api public289+ * @public290 */291 291-app.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;292+app.engine = function engine(ext, fn) {293+ if (typeof fn !== 'function') {294+ throw new Error('callback function required');295+ }296+297+ // get file extension298+ var extension = ext[0] !== '.'299+ ? '.' + ext300+ : ext;301+302+ // store engine303+ this.engines[extension] = fn;304+305 return this;306 };307 308 /**4 unmodified lines313 *314 * @param {String|Array} name315 * @param {Function} fn316 * @return {app} for chaining307- * @api public317+ * @public318 */319 310-app.param = function(name, fn){320+app.param = function param(name, fn) {321 this.lazyrouter();322 323 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+ }327+328 return this;329 }330 331 this._router.param(name, fn);332+333 return this;334 };335 336 /**7 unmodified lines344 *345 * @param {String} setting346 * @param {*} [val]347 * @return {Server} for chaining336- * @api public348+ * @public349 */350 339-app.set = function(setting, val){351+app.set = function set(setting, val) {352 if (arguments.length === 1) {353 // app.get(setting)354 return this.settings[setting];355 }37 unmodified lines393 * was mounted as "/blog" then the394 * return value would be "/blog/admin".395 *396 * @return {String}385- * @api private397+ * @private398 */399 388-app.path = function(){400+app.path = function path() {401 return this.parent402 ? this.parent.path() + this.mountpath403 : '';404 };9 unmodified lines414 * // => true415 *416 * @param {String} setting417 * @return {Boolean}406- * @api public418+ * @public419 */420 409-app.enabled = function(setting){410- return !!this.set(setting);421+app.enabled = function enabled(setting) {422+ return Boolean(this.set(setting));423 };424 425 /**426 * Check if `setting` is disabled.6 unmodified lines433 * // => false434 *435 * @param {String} setting436 * @return {Boolean}425- * @api public437+ * @public438 */439 428-app.disabled = function(setting){440+app.disabled = function disabled(setting) {441 return !this.set(setting);442 };443 444 /**445 * Enable `setting`.446 *447 * @param {String} setting448 * @return {app} for chaining437- * @api public449+ * @public450 */451 440-app.enable = function(setting){452+app.enable = function enable(setting) {453 return this.set(setting, true);454 };455 456 /**457 * Disable `setting`.458 *459 * @param {String} setting460 * @return {app} for chaining449- * @api public461+ * @public462 */463 452-app.disable = function(setting){464+app.disable = function disable(setting) {465 return this.set(setting, false);466 };467 468 /**1 unmodified line470 */471 472 methods.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+ }478 479 this.lazyrouter();480 481 var route = this._router.route(path);8 unmodified lines490 *491 * @param {String} path492 * @param {Function} ...493 * @return {app} for chaining479- * @api public494+ * @public495 */496 482-app.all = function(path){497+app.all = function all(path) {498 this.lazyrouter();499 500 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- });502 503+ for (var i = 0; i < methods.length; i++) {504+ route[methods[i]].apply(route, args);505+ }506+507 return this;508 };509 510 // del -> delete alias12 unmodified lines523 * })524 *525 * @param {String} name526 * @param {String|Function} options or fn511- * @param {Function} fn512- * @api public527+ * @param {Function} callback528+ * @public529 */530 515-app.render = function(name, options, fn){516- var opts = {};531+app.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;538 539 // 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 }544 545 // merge app.locals527- merge(opts, this.locals);546+ merge(renderOptions, this.locals);547 548 // merge options._locals530- if (options._locals) {531- merge(opts, options._locals);549+ if (opts._locals) {550+ merge(renderOptions, opts._locals);551 }552 553 // merge options535- merge(opts, options);554+ merge(renderOptions, opts);555 556 // 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+ }560 561 // primed cache543- if (opts.cache) view = cache[name];562+ if (renderOptions.cache) {563+ view = cache[name];564+ }565 566 // view567 if (!view) {547- view = new (this.get('view'))(name, {568+ var View = this.get('view');569+570+ view = new View(name, {571 defaultEngine: this.get('view engine'),572 root: this.get('views'),573 engines: engines574 });3 unmodified lines578 ? '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 }584 585 // prime the cache563- if (opts.cache) cache[name] = view;586+ if (renderOptions.cache) {587+ cache[name] = view;588+ }589 }590 591 // render592 try {568- view.render(opts, fn);593+ view.render(renderOptions, done);594 } catch (err) {570- fn(err);595+ done(err);596 }597 };598 599 /**13 unmodified lines613 * http.createServer(app).listen(80);614 * https.createServer({ ... }, app).listen(443);615 *616 * @return {http.Server}592- * @api public617+ * @public618 */619 595-app.listen = function(){620+app.listen = function listen() {621 var server = http.createServer(this);622 return server.listen.apply(server, arguments);623 };624 625 /**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+ */631 607-function logerror(err){632+function 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 lines65 - 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`72 73 4.12.4 / 2015-05-172882 unmodified lines