Skip to main content
BufferConstructor.allocUnsafeSlow - node__buffer.d.ts - Node documentation
method BufferConstructor.allocUnsafeSlow

Usage in Deno

```typescript import { type BufferConstructor } from "node:node__buffer.d.ts"; ```
BufferConstructor.allocUnsafeSlow(size: number): Buffer
Allocates a new `Buffer` of `size` bytes. If `size` is larger than [constants.MAX_LENGTH](../.././node__buffer.d.ts/~/constants.MAX_LENGTH) or smaller than 0, `ERR_OUT_OF_RANGE` 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 KiB 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. ```js import { Buffer } from 'node: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.

Parameters

size: number
The desired length of the new `Buffer`.

Return Type