• Node.js Video Tutorials

Node.js - Buffer.alloc() Method



The NodeJS Buffer.alloc() method helps to create a new buffer with the size given. If the size given happens to be greater than buffer.constants.MAX_LENGTH or less than 0, it will throw an error of type: ERR_INVALID_ARG_VALUE. It will throw a typeError if the size given is not a number.

Also creating a buffer using Buffer.alloc() is said to be slower than its alternative Buffer.allocUnsafe(), but it takes care of that the newly created buffer does not have any sensitive data from the previous buffers and also data that was previously allocated for buffer creation.

Syntax

Following is the syntax of the NodeJS Buffer.alloc() Method

Buffer.alloc(size[, fill[, encoding]])

Parameters

This method accepts three parameters. The same is explained below.

  • size −(required) This is integer value and is the length for the buffer to be created.

  • fill −(optional) Here fill is the value you want the buffer to fill with.By default the value is 0.

  • encoding −(optional) The default encoding used is utf8. If you are using a string the encoding is required.

Return value

The method Buffer.alloc() will return the buffer of the given size.

Example

In the example will create a buffer using NodeJS Buffer.alloc() method −

const mybuffer = Buffer.alloc(5);
console.log(mybuffer);

Output

The size of the buffer is 5. Since we have not used fill, the default value 0 is filled in the buffer of size 5.

<Buffer 00 00 00 00 00>

Example

In the example will create a buffer using Buffer.alloc() method and fill variable −

const mybuffer = Buffer.alloc(5, 5);
console.log(mybuffer);

Output

The size of the buffer is 5. Since we have used fill as 5.

<Buffer 05 05 05 05 05>

Example

In the example will create a buffer using Buffer.alloc() method and use the fill as a string value −

const mybuffer = Buffer.alloc(5, 'a');
console.log(mybuffer);
console.log(mybuffer.toString());

Output

The size of the buffer is 5. Since we have used fill is a string: 'a'.

<Buffer 61 61 61 61 61>
aaaaa

Example

In this example will see the error thrown if the size used is not a number.

const mybuffer = Buffer.alloc('1');
console.log(mybuffer);
console.log(mybuffer.toString());

Output

Since the size used is a string, it's an invalid value as size has to be an integer value. When you execute the above program, it will throw an error as shown below.

TypeError [ERR_INVALID_ARG_TYPE]: The "size" argument must be of type number. Received type string
   at Function.alloc (buffer.js:271:3)
   at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:1:25)
   at Module._compile (internal/modules/cjs/loader.js:816:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
   at Module.load (internal/modules/cjs/loader.js:685:32)
   at Function.Module._load (internal/modules/cjs/loader.js:620:12)
   at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
   at internal/main/run_main_module.js:21:11
nodejs_buffer_module.htm
Advertisements