alloc
BufferConstructor.alloc
alloc(size
, fill?
, encoding?
): Buffer
Allocates a new Buffer
of size
bytes. If fill
is undefined
, theBuffer
will be zero-filled.
If size
is larger than constants.MAX_LENGTH or smaller than 0, ERR_INVALID_ARG_VALUE
is thrown.
If fill
is specified, the allocated Buffer
will be initialized by calling buf.fill(fill)
.
If both fill
and encoding
are specified, the allocated Buffer
will be
initialized by calling buf.fill(fill, encoding)
.
Calling Buffer.alloc()
can be measurably slower than the alternative Buffer.allocUnsafe()
but ensures that the newly created Buffer
instance
contents will never contain sensitive data from previous allocations, including
data that might not have been allocated for Buffer
s.
A TypeError
will be thrown if size
is not a number.
Parameters
size
numberRequiredBuffer
.fill
string | number | BufferBuffer
with.encoding
BufferEncodingfill
is a string, this is its encoding.Returns
Since
v5.10.0