DEV Community

Kashyap Patel
Kashyap Patel

Posted on

Node Fundamentals: Buffer

This is a series of posts that will illustrate the what, why and how of Node. I'll be sharing my learnings from a course on Advanced NodeJS by Samer Buna offered on PluralSight. Any code samples tagged or attached will be available at the following repo.

GitHub logo jscomplete / advanced-nodejs

For help, ask in #questions at slack.jscomplete.com

Buffer

Buffer is heavily used in Node to work with binary streams of data. It is a low-level object to represent a sequence of binary data.

A buffer is essentially a chunk of memory allocated outside v8 heap and we can put some data in memory, which can be interpreted in many ways based on the length of each character. That's why there is always a corresponding character-encoding associated with that buffer.

Whatever we place inside a buffer, doesn't have any character encoding, so to read it we need to specify an encoding.

Unlike arrays, once the buffer is allocated, it can't be resized. We can create a buffer in 1 of 3 major ways.

  1. Buffer.alloc(n) - Allocates a 0-filed buffer of n bytes in memory.
  2. Buffer.allocUnsafe(n) - Allocates a buffer of n byte in memory. This can cause a vulnerability as it can contain sensitive information.
  3. Buffer.from() - Allocates a buffer with a value passed in argument.
const string = "touché";
const buffer = Buffer.from("touché");
console.log(string.length) // 6
console.log(buffer.length) // 7

Buffers are useful when we want to read an image file from a TCP stream or a compressed file or any other form of binary data.

Just like arrays and string, we can use operations like includes, slice, indexOf.

In the case of slice, unlike arrays, a sliced copy will use the same memory space.

String Decoder

When converting streams of binary data, use String Decoder module as it handles multi-byte characters much better. It gracefully handles incomplete characters, while calling toString method on buffer doesn't do that.

Top comments (0)