Open In App

Node.js Buffer.alloc() Method

The Buffer.alloc() method is used to create a new buffer object of the specified size. This method is slower than Buffer.allocUnsafe() method but it assures that the newly created Buffer instances will never contain old information or data that is potentially sensitive.

Syntax



Buffer.alloc(size, fill, encoding)

Parameters: This method accept three parameters as mentioned above and described below:

Return Value: This method returns a new initialized Buffer of the specified size. A TypeError will be thrown if the given size is not a number.



Example 1:




// Node.js program to demonstrate the   
// Buffer.alloc() Method
  
// Allocate buffer of given size
// using buffer.alloc() method
var buf = Buffer.alloc(6);
   
// Prints: <Buffer 00 00 00 00 00 00>
console.log(buf);

Output:

<Buffer 00 00 00 00 00 00>

Example 2:




// Node.js program to demonstrate the   
// Buffer.alloc() Method
  
// Allocate buffer of given size
// using buffer.alloc() method
var buf = Buffer.alloc(6, 'a');
   
// Prints <Buffer 61 61 61 61 61>
console.log(buf);

Output:
<Buffer 61 61 61 61 61>

Reference:
https://nodejs.org/docs/latest-v11.x/api/buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding

Article Tags :