# Buffer in Node.js

## What is a Buffer?
- Buffer is a Node.js core module that works with raw binary data. 
- We can directly deal with the buffer using the Buffer module.

## Why Node.js needs Buffers for binary data?
- Everything on the internet is in binary format, so we need something to deal with binary data. Here, the buffer comes into play and saves the day.

## Creating Buffer
We can create the buffer using a 3-way, which are as follows:-

-  **Buffer.from()**

We can create the new buffer using a string, an array, or the buffer itself as parameters to `Buffer.from()` object.
```js 
// 1. From a string

const buf1 = Buffer.from('Hello, Buffer')
console.log(buf1) // <Buffer 45 78 6c ..>
console.log(buf1.toString()) // Hello, Buffer

// 2. From an array

const buf2 = Buffer.from([72, 101, 108, 108, 111])
console.log(buf2.toString()) // Hello

// 3. From another buffer (copying it)

const buf3 = Buffer.from(buf1);
console.log(buf3.toString()) // Hello, Buffer

```

- **Buffer.alloc() and Buffer.allocUnsafe()**

We use this function to create a new Buffer of a given size. 

```js 
1. From Buffer.alloc()

const buf4 = Buffer.alloc(10)
console.log(buf4) // <Buffer 00 00 00  00 00 ...>

2. From Buffer.allocUnsafe()
const buf5 = Buffer.allocUnsafe(10)
console.log(buf5); // <Buffer e8 91 ... random data>

```
#### When to use __alloc()__ or __allocUnsafe()__?
- **alloc()**
  - Slower because it initializes memory with zeros.
  - Safer, since no old data leaks (NodeJS clears/cleans memory before assigning).
- **allocUnsafe()**
  - Faster because it skips initialization (it skips the zero-filling step).
  - Unsafe, because the buffer may contain old memory values until overwritten (NodeJS does not clear/clean memory before assigning).


It depends on the situation, what to use if you need speed and don't need to care about safety use `allocUnsafe` or use `alloc`

> **NOTE:**  
> Don't use `new Buffer()` – it’s **deprecated** and **unsafe**.  
> Use `Buffer.from()`, `Buffer.alloc()`, or `Buffer.allocUnsafe()` instead.

## Reading & Writing Data in Buffers

- **Accessing Bytes**

Each element in the buffer is a byte (0-255).
```js 

const buf = Buffer.from("Hello");
console.log(buf[0]);  // 72 (ASCII for 'H')
console.log(buf[1]);  // 101 (ASCII for 'e')

```

- **Converting Buffer to String**

Buffers can easily convert back and forth with strings, using encodings.

```js 

const buf = Buffer.from("Hello, world!", "utf8");

console.log(buf.toString("utf8"));   // "Hello, world!"
console.log(buf.toString("hex"));    // "48656c6c6f2c20776f726c6421"
console.log(buf.toString("base64")); // "SGVsbG8sIHdvcmxkIQ=="


const buf1 = Buffer.from("Hello", "utf8");
const buf2 = Buffer.from("48656c6c6f", "hex");
const buf3 = Buffer.from("SGVsbG8=", "base64");

console.log(buf1.toString()); // Hello
console.log(buf2.toString()); // Hello
console.log(buf3.toString()); // Hello


```
> **Tips:**  
> - You can perform **array-like operations** on a buffer, such as `slice()`, `copy()`, `concat()`, etc.  
> - Buffers are widely used in **networking (TCP sockets)**, file I/O, and **exchanging data across the web** where raw binary data is required.  

---

##  Conclusion

That’s all for **Buffers in Node.js**.  
We learned how to create buffers, read/write data, and convert between different encodings.  

This is just the beginning! In the upcoming articles, we’ll gonna learn about **Node.js core modules** and eventually put everything together into a real-world project.  

Stay tuned, and follow along for more advanced stuff.  
Till then, happy coding & goodbye.




