Tutorials References Exercises Menu
Paid Courses

Node.js Buffer.allocUnsafe() Method

❮ Buffer Module


Example

Create a 15 bytes buffer object:

var buf = Buffer.allocUnsafe(15);
console.log(buf);
Run example »

Definition and Usage

The Buffer.allocUnsafe() method creates a new buffer object of the specified size.

This method differs from the Buffer.alloc() method because it creates a not-pre-filled buffer, and it may contain information from older buffers. That is why it is called unsafe.

To prevent this buffer from containing old data, you can use the Buffer.fill() method to pre-fill the buffer.


Syntax

 Buffer.allocUnsafe(size);

Parameter Values

Parameter Description
size Required. Specifies the size of the buffer

Technical Details

Return Value: None
Node.js Version: 5.10.0

More Examples

Example

Empty an unsafe buffer:

var buf = Buffer.allocUnsafe(15);
console.log(buf);
//Empty the buffer:
buf.fill(0);
console.log(buf);
Run example »

❮ Buffer Module