Updated February 21, 2023
Definition of js Buffers
Buffers are designed to handle binary data. Buffer is a class in node.js. Buffers are mostly used to work with raw binary data. Buffers also have some methods to work with binary data. The buffers are more look like integer arrays. Buffer in each integer has a size of a byte. It ranges from 0 to 255. As we all know, node.js is nothing but a JavaScript runtime environment for servers. We don’t have any solution for this in JavaScript. Node.js has a global buffer class that helps to work with a binary stream of data. As we know, the buffer class is a subclass of unit8Array.Unit8array class we have in JavaScript.
How Node.js Buffers Works?
Node.js buffers work with a binary stream of data. All types of array methods we can use in buffers as buffers are like integer arrays. First, we need to understand why we got this to work with a buffer in node.js. Node.js buffer class works exactly similar to integer array. Node.js buffer object shows the sequence of bytes to work with. In node.js, we are mostly working with API (Application Programming Interface). When we are working with buffers in node.js, we are not able to adjust the size of it once given. As we have seen earlier, Buffer is a subclass of unit8Array, the same way unit8Array is a subclass of TypeArray. So indirectly, whatever methods are available in TypeArray also exist in Buffers. At the same time, we can also find some difficulties between these two APIs.
How to Create Node.js Buffers?
Buffer class can be created in various ways. In lower node.js versions, those are less than version 6 buffers were get created by using their constructor functions. Here when we were creating buffers, it was get allocated differently, as per the arguments provided at the time of creating the buffer. There are many ways to create a buffer in node.js. We will see each one by one. The first way is to declare a variable and allocate a specified amount of memory to it.
Example
var bufferMemory = Buffer.alloc(4);
This will give us 4 places.
Output
The next way to write buffer is while writing; we can initialize it. Now we will add values to the given buffer.
Example
var bufferMemory = Buffer.from([5,6,4,1]);
Output
For initializing buffer, we used array. And as we know, the contents of an array are integers because it works as an integer array. So generally, when we work with buffer character encoding, id is used as utf-8 by default.
Suppose we passed a string in the above buffer, then it will give a corresponding value of it.
Example
var bufferMemory = Buffer.from("Hello","utf-8");
console.log(bufferMemory);
Output
The above code initializes the string to the binary format. Utf-8 is, by default, a node.js buffer that supports many more types of encoding, but this one is mostly used. There other also like utf16le, latin1, ASCII, binary, hex, base64, etc.
How to Write Node.js Buffers?
In any programming language, when we are writing a program, we write step by step. If you are following this article until now, you must know that we have created a buffer and initialized it. Now let’s see how can we write to buffers. But if you remember, first of all, we have to allocate some memory to it.
Example
var bufferData = Buffer.alloc(32); // here we are allocating memory
// now let’s write something to it
bufferData.write("Hello World", "utf-8"); //we have given string and type as utf-8
Run above two lines, and let’s check the output for it.
Output
How to read Node.js Buffers?
Till now, we have seen how to write to the buffer. Now it’s time to see how to read data from a buffer. We can achieve this with the help of the toString method. It is very simple. Simply write the below code in the node.js command prompt. And enter.
Example
buffer.toString('utf-8');
it will give the string which we wrote above and the remaining memory which was allocated earlier. As we allocated 32.
Output
But if you see closely, we don’t want used memory to show up. For this, we have some methods to get the required output only. We will give the specified range to show up. In short, we are slicing it. Let’s see how can we implement that.
Example
bufferData.toString("utf-8", 0, 11);
Output
If you see in the above example, the first two positions are occupied by our string. We gave indexes no 0 to 11. This contains our required string to be printed. And it returns Hello World as expected. We can also slice the buffer.
Syntax
buffer.slice([start][, end]);
This is having two parameters one starts, which shows a starting position. And other is the end which is the by default length of the buffer. Similarly, you can always check the length of the buffer by following the method.
Syntax
BufferData.length
We have a method to compare with other buffers. Please follow the syntax given below:
Syntax
bufferData.compare(otherBuffer);
Here, another buffer referring to the other created buffer. You can create any buffer, as shown above in the article. Put it in the given syntax above and try on the node.js command prompt. At some point, we need to convert buffer into Json format. This method returns the JSON structure of the buffer.
Example
bufferData.toJSON();
This will Give Jason a Representation of the buffer as below:
Output
Advantages of using Node.js Buffers
- Js buffers used to handle binary data.
- We don’t need to import buffer in node.js with require syntax.
- We always need to deal with binary data when it comes to TCP or a file system; buffer helps here.
- Buffer is a global class in node.js.
- Buffer has many methods to encode different types of data with binary.
Conclusion
Js buffers are very useful in programming. To get more idea about it, one thing is practice. Try each and every possible aspect with different methods specified above. Buffers are really useful to handle data flow.
Recommended Articles
This is a guide to Node.js Buffers. Here we discuss the Introduction, how Node.js Buffers works? and how to create Node.js Buffers using various ways. You may also have a look at the following articles to learn more –