- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
Node.js - Buffer.allocUnsafe() Method
The NodeJS Buffer.allocUnsafe() method helps to create a new buffer with the size given. If the size given happens to be greater than buffer.constants.MAX_LENGTH or less than 0, it will throw an error of type: ERR_INVALID_ARG_VALUE. It will throw a typeError if the size given is not a number.
The buffer created using buffer.allocUnsafe() the memory allocated is not initialized and has contents which are unknown i.e it could have content of older buffers. It is also possible that it might contain sensitive data. For security purposes it is best to use Buffer.alloc(). The old data from the buffer can be eliminated by using the method Buffer.fill().
Syntax
Following is the syntax of the NodeJS Buffer.allocUnsafe() Method −
Buffer.allocUnsafe(size)
Parameters
This method accepts a single parameter. The same is explained below.
size −This is integer value and is the length for the buffer to be created.
Return value
The method Buffer.allocUnsafe() will return a new buffer with the size given.
Example
In the example will create a buffer using NodeJS Buffer.allocUnsafe() method −
const buf = Buffer.allocUnsafe(5); console.log(buf);
Output
The size of the buffer is 5. On executing the above program, it will generate the following output −
<Buffer ff ff ff ff 39>
Example
The example will create a buffer using Buffer.allocUnsafe() method and to fill the value in the buffer will make use of Buffer.fill().
const buf = Buffer.allocUnsafe(5); buf.fill('a') console.log(buf);
Output
The size of the buffer is 5 and we have filled the content of the buffer with value: a. On executing the above program, it will generate the following output −
<Buffer 61 61 61 61 61>
Example
In this example will see the error thrown if the size used is not a number.
const buf = Buffer.allocUnsafe('5'); buf.fill('a') console.log(buf);
Output
Since the size used is a string, it's an invalid value as size has to be an integer value. When you execute the above program, it will throw an error as shown below. On executing the above program, it will generate the following output −
TypeError [ERR_INVALID_ARG_TYPE]: The "size" argument must be of type number. Received type string at Function.allocUnsafe (buffer.js:284:3) at Object.<anonymous> (C:\nodejsProject\src\testbuffer.js:1:20) at Module._compile (internal/modules/cjs/loader.js:816:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10) at Module.load (internal/modules/cjs/loader.js:685:32) at Function.Module._load (internal/modules/cjs/loader.js:620:12) at Function.Module.runMain (internal/modules/cjs/loader.js:877:12) at internal/main/run_main_module.js:21:11
To Continue Learning Please Login
Login with Google