Class Method: Buffer.alloc(size[, fill[, encoding]])
Allocates a new Buffer of size bytes. If fill is undefined, the Buffer will be zero-filled.
const buf = Buffer.alloc(5); console.log(buf); // <Buffer 00 00 00 00 00>
The size must be less than or equal to the value of require('buffer').kMaxLength (on 64-bit architectures, kMaxLength is (2^31)-1). Otherwise, a RangeError is thrown. If a size less than 0 is specified, a zero-length Buffer will be created.
If fill is specified, the allocated Buffer will be initialized by calling buf.fill(fill). See [buf.fill()][] for more information.
const buf = Buffer.alloc(5, 'a'); console.log(buf); // <Buffer 61 61 61 61 61>
If both fill and encoding are specified, the allocated Buffer will be initialized by calling buf.fill(fill, encoding). For example:
const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64'); console.log(buf); // <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
Calling Buffer.alloc(size) can be significantly slower than the alternative Buffer.allocUnsafe(size) but ensures that the newly created Buffer instance contents will never contain sensitive data.
A TypeError will be thrown if size is not a number.
Please login to continue.