perf: remove argument reassignments in request prototype
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 f5a2406 · Browse files · 2 files·+43 added−30 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+43 added−30 removed
- other:+43 added−30 removed
Provenance
- Commit
d414a2d- Tree
3023ee9- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
f5a2406
2 files changed+43 added−30 removed
modified:lib/request.js+42 added-30 removed
8 unmodified lines9 'use strict';10 11 /**12 * Module dependencies.13+ * @private14 */15 16 var accepts = require('accepts');17 var deprecate = require('depd')('express');33 unmodified lines51 * Aliased as `req.header()`.52 *53 * @param {String} name54 * @return {String}54- * @api public55+ * @public56 */57 58 req.get =58-req.header = function(name){59- switch (name = name.toLowerCase()) {59+req.header = function header(name) {60+ var lc = name.toLowerCase();61+62+ switch (lc) {63 case 'referer':64 case 'referrer':65 return this.headers.referrer66 || this.headers.referer;67 default:65- return this.headers[name];68+ return this.headers[lc];69 }70 };71 72 /**38 unmodified lines111 * // => "json"112 *113 * @param {String|Array} type(s)114 * @return {String|Array|Boolean}112- * @api public115+ * @public116 */117 118 req.accepts = function(){119 var accept = accepts(this);4 unmodified lines124 * Check if the given `encoding`s are accepted.125 *126 * @param {String} ...encoding127 * @return {String|Array}125- * @api public128+ * @public129 */130 131 req.acceptsEncodings = function(){132 var accept = accepts(this);8 unmodified lines141 * otherwise you should respond with 406 "Not Acceptable".142 *143 * @param {String} ...charset144 * @return {String|Array}142- * @api public145+ * @public146 */147 148 req.acceptsCharsets = function(){149 var accept = accepts(this);8 unmodified lines158 * otherwise you should respond with 406 "Not Acceptable".159 *160 * @param {String} ...lang161 * @return {String|Array}159- * @api public162+ * @public163 */164 165 req.acceptsLanguages = function(){166 var accept = accepts(this);19 unmodified lines186 * with 4 users when available, not 3.187 *188 * @param {Number} size189 * @return {Array}187- * @api public190+ * @public191 */192 193 req.range = function(size){194 var range = this.get('Range');14 unmodified lines209 *210 * @param {String} name211 * @param {Mixed} [defaultValue]212 * @return {String}210- * @api public213+ * @public214 */215 216 req.param = function param(name, defaultValue) {217 var params = this.params || {};32 unmodified lines250 *251 * req.is('html');252 * // => false253 *251- * @param {String} type252- * @return {Boolean}253- * @api public254+ * @param {String|Array} types...255+ * @return {String|false|null}256+ * @public257 */258 256-req.is = function(types){257- if (!Array.isArray(types)) types = [].slice.call(arguments);258- return typeis(this, types);259+req.is = function is(types) {260+ var arr = types;261+262+ // support flattened arguments263+ if (!Array.isArray(types)) {264+ arr = new Array(arguments.length);265+ for (var i = 0; i < arr.length; i++) {266+ arr[i] = arguments[i];267+ }268+ }269+270+ return typeis(this, arr);271 };272 273 /**274 * Return the protocol string "http" or "https"5 unmodified lines280 * If you're running behind a reverse proxy that281 * supplies https for you this may be enabled.282 *283 * @return {String}272- * @api public284+ * @public285 */286 287 defineGetter(req, 'protocol', function protocol(){288 var proto = this.connection.encrypted16 unmodified lines305 *306 * req.protocol == 'https'307 *308 * @return {Boolean}297- * @api public309+ * @public310 */311 312 defineGetter(req, 'secure', function secure(){301- return 'https' == this.protocol;313+ return this.protocol === 'https';314 });315 316 /**317 * Return the remote address from the trusted proxy.1 unmodified line319 * The is the remote address on the socket unless320 * "trust proxy" is set.321 *322 * @return {String}311- * @api public323+ * @public324 */325 326 defineGetter(req, 'ip', function ip(){327 var trust = this.app.get('trust proxy fn');8 unmodified lines336 * where "proxy2" is the furthest down-stream and "proxy1" and337 * "proxy2" were trusted.338 *339 * @return {Array}328- * @api public340+ * @public341 */342 343 defineGetter(req, 'ips', function ips() {344 var trust = this.app.get('trust proxy fn');12 unmodified lines357 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.358 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.359 *360 * @return {Array}349- * @api public361+ * @public362 */363 364 defineGetter(req, 'subdomains', function subdomains() {365 var hostname = this.hostname;11 unmodified lines377 /**378 * Short-hand for `url.parse(req.url).pathname`.379 *380 * @return {String}369- * @api public381+ * @public382 */383 384 defineGetter(req, 'path', function path() {385 return parse(this).pathname;6 unmodified lines392 * address, the "X-Forwarded-Host" header field will393 * be trusted.394 *395 * @return {String}384- * @api public396+ * @public397 */398 399 defineGetter(req, 'hostname', function hostname(){400 var trust = this.app.get('trust proxy fn');10 unmodified lines411 ? host.indexOf(']') + 1412 : 0;413 var index = host.indexOf(':', offset);414 403- return ~index415+ return index !== -1416 ? host.substring(0, index)417 : host;418 });419 8 unmodified lines428 * Last-Modified and/or the ETag429 * still match.430 *431 * @return {Boolean}420- * @api public432+ * @public433 */434 435 defineGetter(req, 'fresh', function(){436 var method = this.method;15 unmodified lines452 * "Last-Modified" and / or the "ETag" for the453 * resource has changed.454 *455 * @return {Boolean}444- * @api public456+ * @public457 */458 459 defineGetter(req, 'stale', function stale(){460 return !this.fresh;2 unmodified lines463 /**464 * Check if the request was an _XMLHttpRequest_.465 *466 * @return {Boolean}455- * @api public467+ * @public468 */469 470 defineGetter(req, 'xhr', function xhr(){471 var val = this.get('X-Requested-With') || '';460- return 'xmlhttprequest' == val.toLowerCase();472+ return val.toLowerCase() === 'xmlhttprequest';473 });474 475 /**476 * Helper function for creating a getter on an object.477 *478 * @param {Object} obj479 * @param {String} name480 * @param {Function} getter469- * @api private481+ * @private482 */483 function defineGetter(obj, name, getter) {484 Object.defineProperty(obj, name, {485 configurable: true,4 unmodified linesmodified:History.md+1 added
66 unmodified lines67 - perf: remove bitwise operations68 * perf: enable strict mode69 * perf: isolate `app.render` try block70 * perf: remove argument reassignments in application71+ * perf: remove argument reassignments in request prototype72 * perf: remove argument reassignments in `View`73 * perf: use saved reference to `http.STATUS_CODES`74 75 4.12.4 / 2015-05-172882 unmodified lines