Skip to content

Commit

Permalink
Merge pull request #1733 from michelsalib/perfs/buffer-perfs
Browse files Browse the repository at this point in the history
Avoid useless Buffer transformation
  • Loading branch information
simov committed Aug 18, 2015
2 parents e168bc6 + 478e0c2 commit 51b55ac
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions request.js
Expand Up @@ -432,13 +432,18 @@ Request.prototype.init = function (options) {
}

function setContentLength () {
if (!Buffer.isBuffer(self.body) && !Array.isArray(self.body) && typeof self.body !== 'object') {
self.body = new Buffer(self.body)
}
if (!self.hasHeader('content-length')) {
var length = (Array.isArray(self.body))
? self.body.reduce(function (a, b) {return a + b.length}, 0)
: self.body.length
var length
if (typeof self.body === 'string') {
length = Buffer.byteLength(self.body)
}
else if (Array.isArray(self.body)) {
length = self.body.reduce(function (a, b) {return a + b.length}, 0)
}
else {
length = self.body.length
}

if (length) {
self.setHeader('content-length', length)
} else {
Expand Down

0 comments on commit 51b55ac

Please sign in to comment.