Skip to content
Myr

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

2 files+43 added−30 removed

modified:lib/request.js+42 added-30 removed
8 unmodified lines
9'use strict';1011/**12 * Module dependencies.13 * @private14 */1516var accepts = require('accepts');17var deprecate = require('depd')('express');
33 unmodified lines
51 * Aliased as `req.header()`.52 *53 * @param {String} name54 * @return {String}54 * @api public55 * @public56 */5758req.get =58req.header = function(name){59 switch (name = name.toLowerCase()) {59req.header = function header(name) {60 var lc = name.toLowerCase();6162 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};7172/**
38 unmodified lines
111 * // => "json"112 *113 * @param {String|Array} type(s)114 * @return {String|Array|Boolean}112 * @api public115 * @public116 */117118req.accepts = function(){119 var accept = accepts(this);
4 unmodified lines
124 * Check if the given `encoding`s are accepted.125 *126 * @param {String} ...encoding127 * @return {String|Array}125 * @api public128 * @public129 */130131req.acceptsEncodings = function(){132 var accept = accepts(this);
8 unmodified lines
141 * otherwise you should respond with 406 "Not Acceptable".142 *143 * @param {String} ...charset144 * @return {String|Array}142 * @api public145 * @public146 */147148req.acceptsCharsets = function(){149 var accept = accepts(this);
8 unmodified lines
158 * otherwise you should respond with 406 "Not Acceptable".159 *160 * @param {String} ...lang161 * @return {String|Array}159 * @api public162 * @public163 */164165req.acceptsLanguages = function(){166 var accept = accepts(this);
19 unmodified lines
186 * with 4 users when available, not 3.187 *188 * @param {Number} size189 * @return {Array}187 * @api public190 * @public191 */192193req.range = function(size){194 var range = this.get('Range');
14 unmodified lines
209 *210 * @param {String} name211 * @param {Mixed} [defaultValue]212 * @return {String}210 * @api public213 * @public214 */215216req.param = function param(name, defaultValue) {217 var params = this.params || {};
32 unmodified lines
250 *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 */258256req.is = function(types){257 if (!Array.isArray(types)) types = [].slice.call(arguments);258 return typeis(this, types);259req.is = function is(types) {260 var arr = types;261262 // 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 }269270 return typeis(this, arr);271};272273/**274 * Return the protocol string "http" or "https"
5 unmodified lines
280 * 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 */286287defineGetter(req, 'protocol', function protocol(){288 var proto = this.connection.encrypted
16 unmodified lines
305 *306 * req.protocol == 'https'307 *308 * @return {Boolean}297 * @api public309 * @public310 */311312defineGetter(req, 'secure', function secure(){301 return 'https' == this.protocol;313 return this.protocol === 'https';314});315316/**317 * Return the remote address from the trusted proxy.
1 unmodified line
319 * The is the remote address on the socket unless320 * "trust proxy" is set.321 *322 * @return {String}311 * @api public323 * @public324 */325326defineGetter(req, 'ip', function ip(){327 var trust = this.app.get('trust proxy fn');
8 unmodified lines
336 * where "proxy2" is the furthest down-stream and "proxy1" and337 * "proxy2" were trusted.338 *339 * @return {Array}328 * @api public340 * @public341 */342343defineGetter(req, 'ips', function ips() {344 var trust = this.app.get('trust proxy fn');
12 unmodified lines
357 * 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 */363364defineGetter(req, 'subdomains', function subdomains() {365 var hostname = this.hostname;
11 unmodified lines
377/**378 * Short-hand for `url.parse(req.url).pathname`.379 *380 * @return {String}369 * @api public381 * @public382 */383384defineGetter(req, 'path', function path() {385 return parse(this).pathname;
6 unmodified lines
392 * address, the "X-Forwarded-Host" header field will393 * be trusted.394 *395 * @return {String}384 * @api public396 * @public397 */398399defineGetter(req, 'hostname', function hostname(){400 var trust = this.app.get('trust proxy fn');
10 unmodified lines
411 ? host.indexOf(']') + 1412 : 0;413 var index = host.indexOf(':', offset);414403 return ~index415 return index !== -1416 ? host.substring(0, index)417 : host;418});419
8 unmodified lines
428 * Last-Modified and/or the ETag429 * still match.430 *431 * @return {Boolean}420 * @api public432 * @public433 */434435defineGetter(req, 'fresh', function(){436 var method = this.method;
15 unmodified lines
452 * "Last-Modified" and / or the "ETag" for the453 * resource has changed.454 *455 * @return {Boolean}444 * @api public456 * @public457 */458459defineGetter(req, 'stale', function stale(){460 return !this.fresh;
2 unmodified lines
463/**464 * Check if the request was an _XMLHttpRequest_.465 *466 * @return {Boolean}455 * @api public467 * @public468 */469470defineGetter(req, 'xhr', function xhr(){471 var val = this.get('X-Requested-With') || '';460 return 'xmlhttprequest' == val.toLowerCase();472 return val.toLowerCase() === 'xmlhttprequest';473});474475/**476 * Helper function for creating a getter on an object.477 *478 * @param {Object} obj479 * @param {String} name480 * @param {Function} getter469 * @api private481 * @private482 */483function defineGetter(obj, name, getter) {484 Object.defineProperty(obj, name, {485 configurable: true,
4 unmodified lines
modified:History.md+1 added
66 unmodified lines
67 - 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`74754.12.4 / 2015-05-17
2882 unmodified lines