Skip to content
Myr

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

2 files+79 added−55 removed

modified:lib/response.js+78 added-55 removed
76 unmodified lines
77 *78 * @api public79 */8081res.send = function(body){82 var req = this.req;83 var head = 'HEAD' == req.method;84 var type;81res.send = function send(body) {82 var chunk = body;83 var encoding;84 var len;85 var req = this.req;86 var type;8788 // settings89 var app = this.app;9091 // 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 }102103 // 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 }109110 this.statusCode = chunk;111 chunk = http.STATUS_CODES[chunk];112 }113111 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 }135136 // write strings in utf-8130 if ('string' === typeof body) {137 if (typeof chunk === 'string') {138 encoding = 'utf8';139 type = this.get('Content-Type');140141 // 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 }146147 // 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 }154155 // 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 lines
168 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 }174175 // skip body for HEAD176 if (req.method === 'HEAD') {177 this.end();178 }179180 // respond169 this.end((head ? null : body), encoding);181 this.end(chunk, encoding);182183 return this;184};185
9 unmodified lines
195 *196 * @api public197 */198187res.json = function(obj){199res.json = function json(obj) {200 var val = obj;201202 // 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 }217218 // 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);223224 // 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 }228229 return this.send(body);230};231
9 unmodified lines
241 *242 * @api public243 */244229res.jsonp = function(obj){245res.jsonp = function jsonp(obj) {246 var val = obj;247248 // 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 }263264 // 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')];270271 // content-type272 if (!this.get('Content-Type')) {
310 unmodified lines
583 * @api public584 */585586res.set =569res.header = function(field, val){570 if (2 == arguments.length) {587res.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 lines
725 *726 * @api public727 */728711res.redirect = function(url){712 var head = 'HEAD' == this.req.method;713 var status = 302;729res.redirect = function redirect(url) {730 var address = url;731 var body;732 var status = 302;733734 // 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 }744745 // Set location header728 this.location(url);729 url = this.get('Location');746 this.location(address);747 address = this.get('Location');748749 // Support text/{plain,html} by default750 this.format({751 text: function(){
12 unmodified lines
764765 // Respond766 this.statusCode = status;767 this.set('Content-Length', Buffer.byteLength(body));768769 if (this.req.method === 'HEAD') {770 this.end();771 }772750 this.end(head ? null : body);773 this.end(body);774};775776/**777 * Add `field` to Vary. If already present in the Vary set, then
55 unmodified lines
modified:History.md+1 added
19 unmodified lines
20 - 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` methods25264.5.1 / 2014-07-0627==================28
1768 unmodified lines