Node.js | Buffer.allocUnsafeSlow() Method
Buffer is a temporary memory storage which stores the data when it is being moved from one place to another. It’s like the array of integers. Buffer.allocUnsafeSlow() Method allocates a new buffer instance of given size but do not initialize it.
The Buffer.allocUnsafeSlow() method is used to allocate a new Buffer of given size in bytes. If the given size is greater than buffer.constants.MAX_LENGTH or less than 0, ERR_INVALID_OPT_VALUE is thrown. If the size is zero then a zero-length Buffer is created.
This method is different from Buffer.allocUnsafe() method. In allocUnsafe() method, if buffer size is less than 4KB than it automatically cut out the required buffer from a pre-allocated buffer i.e. it does not initialize a new buffer. It saves memory by not allocating many small Buffer instances. But if developer need to hold on some amount of overhead memory for intermediate amount of time, than allocUnsafeSlow() method can be used.
buffer.allocUnsafeSlow( size )
Parameters: This method accepts single parameter size which holds the desired size of the buffer.
Note: This method throws a TypeError if size is not a number.
Below examples illustrate the use of Buffer.allocUnsafeSlow() Method in Node.js:
allocUnsafeSlow() Method <Buffer 01 00 00 00 00 00 00 00 8b ed>
Note: The above program will compile and run by using the
node index.js command.