Buffers in Node.js, How to Use Buffers?

Buffers in Node.js, How to Use Buffers?

Daily short news for you
  • Indeed, there's nothing that wizards can't come up with. awk is a very powerful command for processing files; it can read, search, summarize... text data. Especially for system log files, you just have to call it with... a command.

    jgarzik/sqawk takes the use of awk to "new heights". It applies SQL syntax for querying as well 😆.

    » Read more
  • I forgot to mention that I promised to share my thoughts with everyone after switching to Safari, and just two days later, I had to go back to Chrome. Why?

    First, I would like to point out a few things I liked about Safari, such as its extremely simple interface, which truly focuses on real web browsing, and I found its speed to be on par with Chrome. Additionally, one feature I really enjoyed was the ability to "hide" certain elements you don't like on a particular webpage. This feature is called Hide Distracting Items.

    However, I began to discover some shortcomings when opening Dev Tools—the space that helps developers debug their websites. I must say it was quite basic. It seems that Safari is not designed for debugging. I spent a while trying to figure out how to view the data being sent through the API, or even how to see a fully printed Object from console.log!?

    That alone is enough of a reason for me to return to Chrome. Perhaps Safari is very focused on privacy and security, which makes it difficult to fulfill these requests. On the flip side, if you are doing regular web browsing, you might really enjoy Safari!

    » Read more
  • Let's take a look back at the 30-year journey of JavaScript from its first appearance in 1995 to 2025.

    30 years, the same age as myself 🥱. I've read many blogs about JavaScript, but it seems that Deno is very determined to revive JavaScript once again. They are actively working to reclaim the name JavaScript for the community instead of letting Oracle hold it without doing anything over the years.

    » Read more

What is a Buffer?

A Buffer is a region of memory that represents a fixed-size, raw binary data outside the V8 JavaScript Engine.

You can think of a Buffer as an array of integers, where each integer represents a byte of data.

It is implemented by the Buffer class.

Why do we need Buffers?

Bufffers were introduced to help developers work with binary data in an ecosystem that traditionally only worked with strings instead of binary data.

Buffers are deeply linked with streams. When a stream process receives data faster than it can handle, it will put the data into a buffer.

A simple analogy for a buffer is when you are watching a video on YouTube and there are two red and white lines. When the white line is longer, it means the browser is loading data faster than it is being watched, and the browser will store that data in a buffer.

Video buffering

Creating a Buffer

A Buffer can be created using Buffer.from(), Buffer.alloc(), or Buffer.allocUnsafe():

const buf = Buffer.from('Hey!');
Buffer.from(array);
Buffer.from(arrayBuffer[, byteOffset[, length]]);
Buffer.from(buffer);
Buffer.from(string[, encoding]);

You can also initialize a buffer with a size. The following example creates a Buffer with a size of 1KB:

const buf = Buffer.alloc(1024);
// or
const buf = Buffer.allocUnsafe(1024);

Both alloc and allocUnsafe methods allocate a Buffer of the specified size in bytes. The Buffer created by alloc is initialized with zeroes, and the Buffer created by allocUnsafe is not initialized. This means that while allocUnsafe can be faster than alloc, the allocated memory segment can potentially contain old data.

If old data exists in memory, it can be accessed or leaked when the Buffer memory is read. This is what makes allocUnsafe truly unsafe and requires caution when using it.

Using Buffers

Accessing the content of a buffer

A Buffer is a byte array that can be accessed like an array:

const buf = Buffer.from('Hey!');
console.log(buf[0]); // 72
console.log(buf[1]); // 101
console.log(buf[2]); // 121

Those numbers are Unicode code points representing the characters at the buffer positions (H => 72, e => 101, y => 121).

You can print the whole content of a buffer using the toString() method:

console.log(buf.toString()); // Hey!  

Note: If you initialize a buffer with a fixed size, the previously allocated memory segment will contain random data, not an empty buffer!

Getting the length of a Buffer

Use the length property.

const buf = Buffer.from('Hey!');
console.log(buf.length); // 4

Iterating over the content of a Buffer

const buf = Buffer.from('Hey!');
for (const item of buf) {
  console.log(item); // 72 101 121 33
}

Modifying the content of a Buffer

You can modify the whole content of a Buffer using write():

const buf = Buffer.alloc(4);
buf.write('Hey!');
console.log(buf); // Hey!  

Or you can also modify the content using array-like syntax:

const buf = Buffer.from('Hey!');
buf[1] = 111; // o
console.log(buf.toString()); // Hoy!  

Copying Buffers

Buffers can be copied using the copy() method:

const buf = Buffer.from('Hey!');
let bufcopy = Buffer.alloc(4);
buf.copy(bufcopy);
console.log(bufcopy); // Hey!  

By default, copy() copies the entire Buffer. You can provide up to 3 parameters in order to specify the start position, end position, and length of the new Buffer:

const buf = Buffer.from('Hey!');
let bufcopy = Buffer.alloc(2); // allocate 2 bytes of memory
buf.copy(bufcopy, 0, 0, 2);
console.log(bufcopy.toString()); // 'He'

Slicing a Buffer

A slice of a Buffer still refers to it, meaning that when the Buffer changes, the slice also changes.

Use slice() to create a slice. The first parameter is the starting position and you can specify a second parameter for the end position:

const buf = Buffer.from('Hey!');
buf.slice(0).toString() // Hey!  
const slice = buf.slice(0, 2);
console.log(slice.toString()); // He
buf[1] = 111; // o
console.log(slice.toString()); // Ho

Summary

Buffer is a fixed-size binary data type. JavaScript developers typically don't work with this type of data as often as C, C++, or Go developers (or any programmers who use system programming languages), interacting with memory on a daily basis. Buffers are commonly found in Stream processing - a way of processing file read/write, transmitting data directly, or any type of information exchange from terminal devices efficiently. In the next article, we will explore what Streams are!

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...