2

I have two cases on using node js Buffer in different place:

  1. Buffer accumulation, which I have several buffers and will combine all of them together in the end.
  2. Buffer writer, which I just need to resize the Buffer when it's insufficient anymore to hold more data.

Naturally, I will use Buffer.concat for the first case and Buffer.copy for the second case. But, that can be reversed in which I can use Buffer.copy for the first case and Buffer.concat for the second case.

Implementation 1 Buffer.concat for first case and Buffer.copy for second case:

// first case: buffer accumulation
const result = Buffer.concat([arrayOfBuffers])

// second case: buffer writer
if (oldbuffer.length - offset < newdata.length) {
  const newsize = oldbuffer.length * growthFactor + newdata.length
  const newbuffer = Buffer.alloc(newsize)
  oldbuffer.copy(newbuffer)
  newdata.copy(newbuffer, offset)
  result = newbuffer
}
// newbuffer is the end result

Implementation 2 Buffer.copy for second case and Buffer.concat for first case:

// first case: buffer accumulation
const totalSize = arrayOfBuffers.reduce((size, buff) => size + buff.length, 0)
const result = Buffer.alloc(totalSize)
let offset = 0
for (const buff of arrayOfBuffers) {
  buff.copy(result, offset)
  offset += buff.length
}

// second case: buffer writer
const result = Buffer.concat([oldbuffer, newdata])

I don't know how both concat and copy internally works in node js. So, which method is best for both case?

0

1 Answer 1

1

Looking at the implementation of Buffer.concat: It seems to be doing exactly what you suggested as replacement in implementation 2/first case: Create a new buffer with the calculated total length, then copy all buffers into the output.

So, based on that it seems reasonable to continue to use Buffer.concat directly, because the code is more concise, easier to follow, and describes better what you want to do ("concatenate all these buffers!")

For the writer case: Your suggested implementations aren't equivalent -- the first one would leave space at the end for adding new data, while the second implementation provides exactly the space needed for the existing buffers. If you need the extra space (optimization for expected future writes, for example), then stick to the first variant, otherwise the same argument applies: Using concat is more concise and easier to read.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.