Skip to content
Myr

perf: prevent multiple Buffer creation in res.send

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 112dbb2 · Browse files · 3 files+24 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+24 added−10 removed

  • tests:+4 added−2 removed
  • other:+20 added−8 removed

3 files+24 added−10 removed

modified:lib/response.js+15 added-8 removed
143 unmodified lines
144 }145 }146147 // populate Content-Length148 if (chunk !== undefined && !this.get('Content-Length')) {149 len = Buffer.isBuffer(chunk)150 ? chunk.length151 : Buffer.byteLength(chunk, encoding);148 if (chunk !== undefined) {149 if (!Buffer.isBuffer(chunk)) {150 // convert chunk to Buffer; saves later double conversions151 chunk = new Buffer(chunk, encoding);152 encoding = undefined;153 }154155 len = chunk.length;156 this.set('Content-Length', len);157 }158159 // method check160 var isHead = req.method === 'HEAD';161162 // ETag support156 var etag = len !== undefined && app.get('etag fn');157 if (etag && ('GET' === req.method || 'HEAD' === req.method)) {158 if (!this.get('ETag')) {163 if (len !== undefined && (isHead || req.method === 'GET')) {164 var etag = app.get('etag fn');165 if (etag && !this.get('ETag')) {166 etag = etag(chunk, encoding);167 etag && this.set('ETag', etag);168 }169 }
9 unmodified lines
179 chunk = '';180 }181182 // skip body for HEAD176 if (req.method === 'HEAD') {183 if (isHead) {184 this.end();185 }186187 // respond
652 unmodified lines
modified:test/res.send.js+4 added-2 removed
510 unmodified lines
511 it('should send custom ETag', function(done){512 var app = express()513514 app.set('etag', function(body, encoding){515 body.should.equal('hello, world!')516 encoding.should.equal('utf8')515 var chunk = !Buffer.isBuffer(body)516 ? new Buffer(body, encoding)517 : body;518 chunk.toString().should.equal('hello, world!')519 return '"custom"'520 });521522 app.use(function(req, res){
29 unmodified lines
modified:History.md+5 added
1unreleased2==========34 * perf: prevent multiple `Buffer` creation in `res.send`564.6.1 / 2014-07-127==================89 * fix `subapp.mountpath` regression for `app.use(subapp)`
1804 unmodified lines