Node.js Tutorial

Node.js Buffers

Node.js Buffers
Node.js Buffers

Node.js Buffers

In Node.js, the Buffer class is used to handle raw binary data. Buffers represent fixed-size blocks of memory outside the V8 JavaScript heap and are commonly used for file system operations, TCP streams, network communication, and low-level byte manipulation.

The Buffer class is globally available in Node.js, so you do not need to import it before using it.

Creating Buffers in Node.js

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

The older new Buffer() API is deprecated. Modern Node.js applications should use Buffer.alloc() and Buffer.from() because they provide safer and clearer ways to create buffers.

// Allocate an empty 10-byte buffer (zero-filled)
const buf1 = Buffer.alloc(10);

// Create a buffer from an array of bytes
const buf2 = Buffer.from([10, 20, 30, 40, 50]);

// Create a buffer from a string
const buf3 = Buffer.from('Simply Easy Learning', 'utf-8');

Writing Data to Buffers

You can use the write() method to store string data inside a buffer.

const buf = Buffer.alloc(256);

const len = buf.write("Simply Easy Learning");

console.log("Octets written:", len);
// Output: Octets written: 20

The write() method returns the number of bytes written to the buffer.

Reading Data from Buffers

Buffer data can be converted back into a readable string using the toString() method. You can also specify an encoding and a range of bytes to read.

const buf = Buffer.alloc(26);

for (let i = 0; i < 26; i++) {
  buf[i] = i + 97;
}

console.log(buf.toString('ascii'));
// abcdefghijklmnopqrstuvwxyz

console.log(buf.toString('ascii', 0, 5));
// abcde

console.log(buf.toString('utf8', 0, 5));
// abcde

Useful Node.js Buffer Methods

Method / PropertyDescription
Buffer.alloc(size)Creates a zero-filled buffer of the specified size.
Buffer.allocUnsafe(size)Creates a buffer faster without initializing its memory. The buffer may contain old memory data, so it should be used carefully.
Buffer.from(string | array)Creates a buffer from a string, array, or other supported data.
buf.lengthReturns the size of the buffer in bytes.
buf.toString(encoding)Converts buffer data into a string using the specified encoding.
Buffer.concat([buf1, buf2])Combines multiple buffers into a single buffer.

Why Buffers Are Important in Node.js

Buffers are especially useful when working with binary data such as files, images, network packets, and streams. Since Node.js frequently performs I/O and network operations, buffers provide an efficient way to process data in its raw byte form.

Download New Real Time Projects:- Click here

Conclusion

Buffers are an important part of Node.js for handling raw binary data efficiently. In modern Node.js development, use Buffer.alloc() and Buffer.from() instead of the deprecated new Buffer() constructor. Understanding buffers is essential when working with files, streams, networking, and other low-level data operations.

Keywords

Node.js Buffers, Node.js Buffer, Buffer in Node.js, Node.js Buffer Tutorial, Buffer.alloc, Buffer.from, Buffer.write, Buffer.toString, Node.js binary data, Node.js streams

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us