Node JS Buffers Tutorial for Beginners and Pros

node js buffers

Node JS Buffers store raw binary data in memory. This guide shows how to create them, work with them, and use them with real examples.

Understand Buffers in Node JS

A buffer in Node JS holds raw binary data in memory. It acts like a container that stores bytes directly. Node JS creates buffers outside the V8 heap for speed.

Buffers handle binary data that comes from files or network sockets. They store bytes fast and avoid slow string conversions. They help manage large data in parts.

Node JS gives many ways to create a buffer. You can use Buffer.alloc to make an empty buffer with a fixed size. You can also use Buffer.from to make a buffer from an array or string.

const buf1 = Buffer.alloc(10); 
const buf2 = Buffer.from('Hello');

This code makes one empty buffer with ten bytes and another buffer from a string.

You can read bytes from a buffer with index access or methods. You can also write data into a buffer with methods like write.

const buf = Buffer.alloc(5);
buf.write('Hi');
console.log(buf.toString());

This code writes two characters into the buffer and prints them back as a string.

Buffer Methods and Properties

Below are main methods and properties with simple examples.

  • Buffer.alloc(size) creates a buffer of given size.
  • Buffer.from(data) creates a buffer from data.
  • buf.write(string, offset) writes string at offset.
  • buf.toString(encoding, start, end) returns buffer data as string.
  • buf.length shows number of bytes in buffer.
  • buf.slice(start, end) returns a new buffer view.
  • Buffer.concat(list) joins many buffers into one.
const b1 = Buffer.from('One');
const b2 = Buffer.from('Two');
const b3 = Buffer.concat([b1, b2]);
console.log(b3.toString());

This code makes two buffers and joins them into one buffer.

How to Convert Buffers to Strings in Node JS

You can call toString on a buffer to convert it to text. You can pass encoding like utf8 or hex to choose format.

const buf = Buffer.from('Text here');
const text = buf.toString('utf8');
console.log(text);

This code creates a buffer from text and then converts it back to a string with utf8 encoding.

Understand the Difference Between Buffers and Streams

A buffer stores data in memory at once. A stream handles data piece by piece. Buffers suit small or fixed data. Streams suit large or continuous data.

AspectBufferStream
Data loadAll at once in memoryPiece by piece
Use caseSmall files or packetsLarge files or live data

Streams save memory for large transfers while buffers are simple for small data.

Examples

Create and Write to Buffer:

const buf = Buffer.alloc(5);
buf.write('Hey');
console.log(buf.toString());

This example makes an empty buffer, writes a short string into it, and prints the result as text.

Combine Buffers from Different Sources:

const part1 = Buffer.from('Node ');
const part2 = Buffer.from('JS');
const result = Buffer.concat([part1, part2]);
console.log(result.toString());

This example joins two buffers into one and then converts the combined buffer to a readable string.

Slice and Modify a Buffer:

const main = Buffer.from('BufferData');
const slice = main.slice(0,6);
console.log(slice.toString());

This example takes a slice of the buffer and shows only part of the data as a string.

Wrapping Up

You learned what buffers are in Node JS and why they matter for binary data. You also saw how to create, write, read, convert, and join buffers.

Here is a quick recap:

  • Buffers hold raw data in memory, methods let you manage that data, and streams differ because they handle chunks over time.

FAQs

What are Node JS Buffers and why are they used?

Node JS buffers are used to handle raw binary data directly. They are especially useful when working with streams, files, and TCP data.
  • Store raw binary data outside V8 heap
  • Efficiently process I/O operations
  • Convert between binary and string formats

How to create a Buffer in Node JS?

You can create a buffer using <Buffer.alloc>, <Buffer.from>, or <Buffer.allocUnsafe>. Example:

const buf1 = Buffer.alloc(10); // creates empty buffer of 10 bytes
const buf2 = Buffer.from('Hello'); // creates buffer from string

How do you convert a Buffer to a string in Node JS?

Use <buf.toString()> method to convert a buffer into a string. Example:

const buf = Buffer.from('NodeJS Buffers');
console.log(buf.toString()); // Output: NodeJS Buffers

What are common Buffer methods in Node JS?

Some commonly used <Buffer> methods include:
  • Buffer.alloc(size) – Create buffer of fixed size
  • Buffer.from(data) – Create buffer from data
  • buf.write(string) – Write string into buffer
  • buf.toString() – Convert buffer to string
  • Buffer.concat() – Merge multiple buffers

Similar Reads

Node JS Events in Depth with Examples

Node JS runs code in an event-based model and uses events to drive tasks. An event is a signal that…

Node.js Modules in Depth with Examples

This guide shows Node.js modules, how they work, how to import and export them, and why you use them. What…

Node.js Hello World: Write Your First Program

In this quick tutorial, you will start Node.js tutorials with a Hello World program. You will learn how to set…

Node.js Streams Guide: How to Handle Data with Examples

Streams pass data in parts, so the app stays fast. This article explains Node.js Streams and how to use them.…

Node.js File System with Examples

Node.js file system lets you read, write, update, and manage files and folders with built-in methods. Understand the Node.js File…

Installing Node js on Windows, Ubuntu, and MacOS

Installing node js on Windows, Ubuntu, or macOS. You need to pick the version of Node.js that you want to…

Node js NPM: How to Publish and Install JS Packages

Node Package Manager (NPM) is an essential tool at the heart of the Node.js ecosystem. This is the defacto package…

Node Version Manager (NVM): Manage Node.js Versions

NVM (Node Version Manager ) is a command-line tool used to manage multiple versions of Node.js on a single machine.…

What is NodeJS? An Introduction to Node.js

What is Nodejs? Node.js is a powerful cross-platform JavaScript runtime that enables developers to execute JavaScript code on the server-side.…

Node.js REPL: The Complete Tutorial for Beginners

Node.js REPL runs JavaScript code in an instant test space inside your terminal. What is Node.js REPL? Node.js REPL is…

Previous Article

Component-driven HTML Guide with Examples

Next Article

JavaScript valueOf Function: How it Works with Examples

Write a Comment

Leave a Comment

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


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.