import { Buffer } from "https://deno.land/std@0.125.0/node/_buffer.d.ts";
Allocates a new Buffer
of size
bytes. If size
is larger than constants.MAX_LENGTH or smaller than 0, ERR_INVALID_ARG_VALUE
is thrown. A zero-length Buffer
is created
if size
is 0.
The underlying memory for Buffer
instances created in this way is not
initialized. The contents of the newly created Buffer
are unknown and_may contain sensitive data_. Use buf.fill(0)
to initialize
such Buffer
instances with zeroes.
When using Buffer.allocUnsafe()
to allocate new Buffer
instances,
allocations under 4 KB are sliced from a single pre-allocated Buffer
. This
allows applications to avoid the garbage collection overhead of creating many
individually allocated Buffer
instances. This approach improves both
performance and memory usage by eliminating the need to track and clean up as
many individual ArrayBuffer
objects.
However, in the case where a developer may need to retain a small chunk of
memory from a pool for an indeterminate amount of time, it may be appropriate
to create an un-pooled Buffer
instance using Buffer.allocUnsafeSlow()
and
then copying out the relevant bits.
import { Buffer } from 'buffer';
// Need to keep around a few small chunks of memory.
const store = [];
socket.on('readable', () => {
let data;
while (null !== (data = readable.read())) {
// Allocate for retained data.
const sb = Buffer.allocUnsafeSlow(10);
// Copy the data into the new allocation.
data.copy(sb, 0, 0, 10);
store.push(sb);
}
});
A TypeError
will be thrown if size
is not a number.