9

Say I have a Buffer:

let b = Buffer.from('');

how can I append to b? Is the only way to create a new Buffer?

b = Buffer.concat([b, z]);

on the same subject, is there a way to create a dynamic sized buffer, or should I use Array instead?

1 Answer 1

14

To create a dynamic buffer use an array then concat the array similar to this:

let chunks = []

stream
  .on('data', chunk => chunks.push(chunk))
  .on('close', () => console.log(Buffer.concat(chunks)))
Sign up to request clarification or add additional context in comments.

2 Comments

I would think Buffer.from(chunks) makes more sense? idk
It might work, but all the examples that I have seen use concat: stackoverflow.com/a/10356183/1778465

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.