Skip to content

Commit

Permalink
Merge pull request #239 from pfreixes/avoid-mutliple-buffer-allocation
Browse files Browse the repository at this point in the history
Preallocate buffer size when reading data
  • Loading branch information
joeybaker committed Feb 15, 2022
2 parents 82e0343 + ef3a4ee commit adc6e49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
35 changes: 28 additions & 7 deletions lib/eventsource.js
Expand Up @@ -15,6 +15,8 @@ var colon = 58
var space = 32
var lineFeed = 10
var carriageReturn = 13
// Beyond 256KB we could not observe any gain in performance
var maxBufferAheadAllocation = 1024 * 256

function hasBom (buf) {
return bom.every(function (charCode, index) {
Expand Down Expand Up @@ -179,19 +181,36 @@ function EventSource (url, eventSourceInitDict) {

// text/event-stream parser adapted from webkit's
// Source/WebCore/page/EventSource.cpp
var isFirst = true
var buf
var newBuffer
var startingPos = 0
var startingFieldLength = -1
var newBufferSize = 0
var bytesUsed = 0

res.on('data', function (chunk) {
buf = buf ? Buffer.concat([buf, chunk]) : chunk
if (isFirst && hasBom(buf)) {
buf = buf.slice(bom.length)
if (!buf) {
buf = chunk
if (hasBom(buf)) {
buf = buf.slice(bom.length)
}
bytesUsed = buf.length
} else {
if (chunk.length > buf.length - bytesUsed) {
newBufferSize = (buf.length * 2) + chunk.length
if (newBufferSize > maxBufferAheadAllocation) {
newBufferSize = buf.length + chunk.length + maxBufferAheadAllocation
}
newBuffer = Buffer.alloc(newBufferSize)
buf.copy(newBuffer, 0, 0, bytesUsed)
buf = newBuffer
}
chunk.copy(buf, bytesUsed)
bytesUsed += chunk.length
}

isFirst = false
var pos = 0
var length = buf.length
var length = bytesUsed

while (pos < length) {
if (discardTrailingNewline) {
Expand Down Expand Up @@ -235,8 +254,10 @@ function EventSource (url, eventSourceInitDict) {

if (pos === length) {
buf = void 0
bytesUsed = 0
} else if (pos > 0) {
buf = buf.slice(pos)
buf = buf.slice(pos, bytesUsed)
bytesUsed = buf.length
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/eventsource_test.js
Expand Up @@ -454,7 +454,7 @@ describe('Parser', function () {
})

it('parses relatively huge messages efficiently', function (done) {
this.timeout(1000)
this.timeout(200)

createServer(function (err, server) {
if (err) return done(err)
Expand Down

0 comments on commit adc6e49

Please sign in to comment.