Open In App

What are buffers in Node.js ?

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss what is the buffer in Node.js and what it uses it. Node.js is one of the most popular run time environments for javascript and the javascript are not properly handle the data so the solution for handling the data in an effective way the Node.js provide a global module called Buffer. and a Buffer is also a global object so we can use without writing any other code for import or require.

Approach: Firstly we understand what is the buffer and after we will discuss what is the use cases where we can use a buffer and lastly we will discuss all the methods in the buffer module and their uses.

Buffer: In Node.js to manipulate a stream of binary data, the buffer module can be included in the code. However, the buffer is a global object in Node.js, hence it is not required to import it in code using the required method.

It is a temporary memory in RAM that manages a block of data and sends it for processing, a buffer is a suitable very small-sized block for binary data, if the buffer is full then the data is directly sent for processing. Sometimes buffer is used as middleware between the processing of data and incoming because we need a processor for the processing of data but sometimes processor is busy in some other task so we need to transfer the data somewhere and need to be relocated. This requirement is met by the buffer. The Buffer class in Node.js provides access to allocate and use a buffer. And the buffer class is a global class so there is no need to import it. If you want to know more about Buffer then check out this article.

 

Use case: A buffer is a global object for handling binary data streams so there is a lot of use case where we can use it.

  • For Creating a buffer stream the handling a custom user binary data.
  • Check the custom binary data are actual binary data are matched or not.
  • Converting binary data to a readable String or JSON.
  • For rewriting an existing binary data stream.

Methods of buffer module: Listed below are some common methods and properties of the Buffer module.

1. alloc() Method: It creates a Buffer object of the given length.

Javascript




let buff = new Buffer.alloc(5);
console.log(buff);


Output:

2. equals() Method: It compares two buffer objects. Returns true if the object match else returns false.

 

Javascript




let name1 = new Buffer.alloc(4, "Name");
let name2 = new Buffer.alloc(4, "Name");
console.log(new Buffer.from(name1).equals(name2));


Output:

3. copy() Method: It copies the given number of bytes of a buffer object.

Javascript




let buff = new Buffer.alloc(5, "Geeks");
let name1 = new Buffer.alloc(5, "Name");
buff.copy(name1);
console.log(name1.toString());


Output:

4. length Property: Return the length of a buffer object in bytes.

Javascript




let buff = new Buffer.alloc(5, 'ABCDE');
console.log(buff.length)


 

Output:

5. toString() Method: It returns a string form of a buffer object.

Javascript




let name2 = new Buffer.alloc(3, "GFG");
console.log(name2);
console.log(name2.toString());


Output:

6. toJSON() Method: It returns a JSON form of a buffer object.

Javascript




let myJson = new Buffer.alloc(10, { name: 'GFG' });
console.log(myJson.toJSON());


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads