perf: fix arguments reassign deopt in some res methods
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 997a558 · Browse files · 2 files·+79 added−55 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+79 added−55 removed
- other:+79 added−55 removed
Provenance
- Commit
c6e6203- Tree
b5d93ff- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
997a558
2 files changed+79 added−55 removed
modified:lib/response.js+78 added-55 removed
76 unmodified lines77 *78 * @api public79 */80 81-res.send = function(body){82- var req = this.req;83- var head = 'HEAD' == req.method;84- var type;81+res.send = function send(body) {82+ var chunk = body;83 var encoding;84 var len;85+ var req = this.req;86+ var type;87 88 // settings89 var app = this.app;90 91 // allow status / body92- if (2 == arguments.length) {92+ if (arguments.length === 2) {93 // res.send(body, status) backwards compat94- if ('number' != typeof body && 'number' == typeof arguments[1]) {94+ if (typeof arguments[0] !== 'number' && typeof arguments[1] === 'number') {95 deprecate('res.send(body, status): Use res.send(status, body) instead');96 this.statusCode = arguments[1];97 } else {98- this.statusCode = body;99- body = arguments[1];98+ this.statusCode = arguments[0];99+ chunk = arguments[1];100 }101 }102 103 // disambiguate res.send(status) and res.send(status, num)104- if (typeof body === 'number' && arguments.length === 1) {104+ if (typeof chunk === 'number' && arguments.length === 1) {105 // res.send(status) will set status message as text string106- this.get('Content-Type') || this.type('txt');107- this.statusCode = body;108- body = http.STATUS_CODES[body];106+ if (!this.get('Content-Type')) {107+ this.type('txt');108+ }109+110+ this.statusCode = chunk;111+ chunk = http.STATUS_CODES[chunk];112 }113 111- switch (typeof body) {114+ switch (typeof chunk) {115 // string defaulting to html116 case 'string':114- if (!this.get('Content-Type')) this.type('html');117+ if (!this.get('Content-Type')) {118+ this.type('html');119+ }120 break;121 case 'boolean':122 case 'number':123 case 'object':119- if (null == body) {120- body = '';121- } else if (Buffer.isBuffer(body)) {122- this.get('Content-Type') || this.type('bin');124+ if (chunk === null) {125+ chunk = '';126+ } else if (Buffer.isBuffer(chunk)) {127+ if (!this.get('Content-Type')) {128+ this.type('bin');129+ }130 } else {124- return this.json(body);131+ return this.json(chunk);132 }133 break;134 }135 136 // write strings in utf-8130- if ('string' === typeof body) {137+ if (typeof chunk === 'string') {138 encoding = 'utf8';139 type = this.get('Content-Type');140 141 // reflect this in content-type135- if ('string' === typeof type) {142+ if (typeof type === 'string') {143 this.set('Content-Type', setCharset(type, 'utf-8'));144 }145 }146 147 // populate Content-Length141- if (undefined !== body && !this.get('Content-Length')) {142- len = Buffer.isBuffer(body)143- ? body.length144- : Buffer.byteLength(body, encoding);148+ if (chunk !== undefined && !this.get('Content-Length')) {149+ len = Buffer.isBuffer(chunk)150+ ? chunk.length151+ : Buffer.byteLength(chunk, encoding);152 this.set('Content-Length', len);153 }154 155 // ETag support156 var etag = len !== undefined && app.get('etag fn');157 if (etag && ('GET' === req.method || 'HEAD' === req.method)) {158 if (!this.get('ETag')) {152- etag = etag(body, encoding);159+ etag = etag(chunk, encoding);160 etag && this.set('ETag', etag);161 }162 }163 4 unmodified lines168 if (204 == this.statusCode || 304 == this.statusCode) {169 this.removeHeader('Content-Type');170 this.removeHeader('Content-Length');171 this.removeHeader('Transfer-Encoding');165- body = '';172+ chunk = '';173 }174 175+ // skip body for HEAD176+ if (req.method === 'HEAD') {177+ this.end();178+ }179+180 // respond169- this.end((head ? null : body), encoding);181+ this.end(chunk, encoding);182 183 return this;184 };185 9 unmodified lines195 *196 * @api public197 */198 187-res.json = function(obj){199+res.json = function json(obj) {200+ var val = obj;201+202 // allow status / body189- if (2 == arguments.length) {203+ if (arguments.length === 2) {204 // res.json(body, status) backwards compat191- if ('number' == typeof arguments[1]) {205+ if (typeof arguments[1] === 'number') {206 this.statusCode = arguments[1];193- if (typeof obj === 'number') {207+ if (typeof arguments[0] === 'number') {208 deprecate('res.json(obj, status): Use res.json(status, obj) instead');209 } else {210 deprecate('res.json(num, status): Use res.status(status).json(num) instead');211 }212 } else {199- this.statusCode = obj;200- obj = arguments[1];213+ this.statusCode = arguments[0];214+ val = arguments[1];215 }216 }217 218 // settings219 var app = this.app;220 var replacer = app.get('json replacer');221 var spaces = app.get('json spaces');208- var body = JSON.stringify(obj, replacer, spaces);222+ var body = JSON.stringify(val, replacer, spaces);223 224 // content-type225+ if (!this.get('Content-Type')) {211- this.get('Content-Type') || this.set('Content-Type', 'application/json');226+ this.set('Content-Type', 'application/json');227+ }228 229 return this.send(body);230 };231 9 unmodified lines241 *242 * @api public243 */244 229-res.jsonp = function(obj){245+res.jsonp = function jsonp(obj) {246+ var val = obj;247+248 // allow status / body231- if (2 == arguments.length) {249+ if (arguments.length === 2) {250 // res.json(body, status) backwards compat233- if ('number' == typeof arguments[1]) {251+ if (typeof arguments[1] === 'number') {252 this.statusCode = arguments[1];235- if (typeof obj === 'number') {253+ if (typeof arguments[0] === 'number') {254 deprecate('res.jsonp(obj, status): Use res.jsonp(status, obj) instead');255 } else {256 deprecate('res.jsonp(num, status): Use res.status(status).jsonp(num) instead');257 }258 } else {241- this.statusCode = obj;242- obj = arguments[1];259+ this.statusCode = arguments[0];260+ val = arguments[1];261 }262 }263 264 // settings265 var app = this.app;266 var replacer = app.get('json replacer');267 var spaces = app.get('json spaces');250- var body = JSON.stringify(obj, replacer, spaces);268+ var body = JSON.stringify(val, replacer, spaces);269 var callback = this.req.query[app.get('jsonp callback name')];270 271 // content-type272 if (!this.get('Content-Type')) {310 unmodified lines583 * @api public584 */585 586 res.set =569-res.header = function(field, val){570- if (2 == arguments.length) {587+res.header = function header(field, val) {588+ if (arguments.length === 2) {589 if (Array.isArray(val)) val = val.map(String);590 else val = String(val);591 if ('content-type' == field.toLowerCase() && !/;\s*charset\s*=/.test(val)) {592 var charset = mime.charsets.lookup(val.split(';')[0]);132 unmodified lines725 *726 * @api public727 */728 711-res.redirect = function(url){712- var head = 'HEAD' == this.req.method;713- var status = 302;729+res.redirect = function redirect(url) {730+ var address = url;731 var body;732+ var status = 302;733 734 // allow status / url717- if (2 == arguments.length) {718- if ('number' == typeof url) {719- status = url;720- url = arguments[1];735+ if (arguments.length === 2) {736+ if (typeof arguments[0] === 'number') {737+ status = arguments[0];738+ address = arguments[1];739 } else {740 deprecate('res.redirect(ur, status): Use res.redirect(status, url) instead');741 status = arguments[1];742 }743 }744 745 // Set location header728- this.location(url);729- url = this.get('Location');746+ this.location(address);747+ address = this.get('Location');748 749 // Support text/{plain,html} by default750 this.format({751 text: function(){12 unmodified lines764 765 // Respond766 this.statusCode = status;767 this.set('Content-Length', Buffer.byteLength(body));768+769+ if (this.req.method === 'HEAD') {770+ this.end();771+ }772+750- this.end(head ? null : body);773+ this.end(body);774 };775 776 /**777 * Add `field` to Vary. If already present in the Vary set, then55 unmodified linesmodified:History.md+1 added
19 unmodified lines20 - add `CONNECT`21 * deps: parseurl@~1.1.322 - faster parsing of href-only URLs23 * deps: path-to-regexp@0.1.324+ * perf: fix arguments reassign deopt in some `res` methods25 26 4.5.1 / 2014-07-0627 ==================28 1768 unmodified lines