Computer scienceBackendNode.jsCore ConceptsInternal modulesBuffers

What is buffer?

5 minutes read

JavaScript is a great language for managing Unicode-encoded strings, but it doesn't work well with binary data. That's why we need buffer. Buffer is a data type used to work with binary data directly. They are particularly useful when you need to read, write, or manipulate binary data, such as when working with file I/O, network communication, or when dealing with binary data formats like images or audio files.

Creating buffers

How can we use a buffer, and what does it look like? Actually, it is easy to create; let's dive into it.

const buffer = Buffer.alloc(4, 1)

console.log(buffer); // <Buffer 01 01 01 01>

The first method is alloc(). It creates a buffer and allocates a size to it. This method is advisable when we need to generate a buffer with a determined size and initialize its values.The first argument of method is required as it determines the size of the buffer. The second argument is optional; if a value is specified, it is used to populate the buffer; otherwise, the buffer is filled with zeros. The last one is also optional. When the buffer values consist of strings, this parameter defines the encoding, with the default encoding being UTF-8."

const buffer1 = Buffer.from([0, 10, 20, 30]);

console.log(buffer1); // // <Buffer 00 0a 14 1e>

Another method is from(). As you can see, it accepts an array as an argument, and it can also accept a string or buffer itself. When you want to create a buffer from existing data, you should use from().

You may be surprised, why we see <Buffer 00 0a 14 1e> instead of just <Buffer 0 10 20 30>. That's because Node.js converts numbers to the hexadecimal number system.

Buffer methods

Buffer has several methods and properties for managing and manipulating binary data. Here are some common methods and operations associated with buffers in Node.js:

let buffer = Buffer.from('abc');
let bufferToString = buffer.toString();

console.log(buffer);  // <Buffer 61 62 63>
console.log(bufferToString);  // abc

The method toString() is used to convert a buffer to a string. It actually has three optional arguments. The first one is encoding, which is 'utf8' by default. The second argument is the starting point, and the third one is the ending point.

let buffer = Buffer.from('Maksim');
let slicedBuffer = buffer.slice(3,6);

console.log(slicedBuffer.toString()); // sim

The slice() method works the same way as slice() with arrays (as I suppose you've already mentioned many common buffer operations with arrays). It returns a sliced buffer, where the first parameter signifies the starting point and the second parameter indicates the ending point.

let buffer = Buffer.from([1,2,3,44,55]);

console.log(buffer.length); // 5

As you can see, this method returns the length of a buffer.

In general, the buffer in Node.js offers a wide range of methods. Some frequently used methods include write(), readUInt8(), copy(), fill(), and toJSON(). You can find a comprehensive list of all the available methods in the Node.js documentation on buffer.

Use cases and pitfalls of buffers

We understand that buffers are a crucial feature for working with binary data. In which cases can we use them?

  • Files Input/Output. Of course, buffers are often used for reading and writing binary data from files.
  • Network Operations. Buffers are essential for sending and receiving binary data over network sockets, such as in HTTP requests and responses.
  • Binary Data Manipulation. Also buffers provide a way to work with binary data directly, enabling tasks like encoding and decoding data, hashing, encryption, and compression.

All things in the world have some cons, and buffers are no exception.

  • Size Limitations. The limitation on buffer size refers to the initial size defined during allocation. While buffers can be adjusted in size dynamically, this process can incur significant costs in both memory usage and performance.
  • Memory Usage. Buffers can consume a significant amount of memory, especially when working with large files or network streams.
  • Encoding and Decoding. Using the wrong encoding can lead to data corruption.

Conclusion

Buffers in Node.js serve as a fundamental tool for efficiently handling binary data. They are especially valuable when dealing with tasks like file I/O, network communication, and binary data formats such as images and audio files. Additionally, buffers come with a range of methods for managing and manipulating binary data. However, it's important to be mindful of their limitations and encoding considerations to use them effectively in various applications involving binary data processing.

4 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo