KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Recap Quiz

5 Questions

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Recap Quiz

5 Questions

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Recap Quiz

5 Questions

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Recap Quiz

5 Questions

Node.js Buffer

10 min

Node.js Streams

10 min

Node.js Crypto

10 min

Recap Quiz

5 Questions

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

5 Questions

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Recap Quiz

5 Questions

Node.js DNS Module

10 min

Node.js Query String

10 min

MySQL Connect

10 min

Recap Quiz

5 Questions

MySQL Create Database

10 min

MySQL Order By

10 min

Recap Quiz

5 Questions

MongoDB Intro

10 min

Recap Quiz

5 Questions

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

5 Questions

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

Recap Quiz

5 Questions

MongoDB Delete

10 min

MongoDB Update

10 min

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

Node.jsNode.js Tutorial
Lesson

Node.js Buffer

10 min reading
Free Course

Node.js Buffer: Handling Raw Binary Data Streams & Encodings

The Buffer class in Node.js provides instances to store raw binary data allocated outside the V8 JavaScript heap in fixed-length, contiguous memory arrays. Buffers are essential when dealing with file streams, network packets, and cryptographic keys.

Binary Data Allocation & Memory Layout

flowchart LR
    A["Raw Binary Stream / Disk Data"] --> B["Fixed-Size Buffer Allocation (C++ Memory Heap)"]
    B --> C["Encodings: UTF-8 / Hex / Base64 / Binary"]
    C --> D["JavaScript String & Array Views"]

Practical Code Example

import { Buffer } from 'node:buffer';

// 1. Allocating buffers
const buf1 = Buffer.alloc(10); // 10 bytes initialized with zeros
const buf2 = Buffer.from('Hello Node.js Buffer!', 'utf-8');

console.log(`Buffer 2 Byte Length: ${buf2.length}`); // 21 bytes
console.log('Hex representation:', buf2.toString('hex'));
console.log('Base64 representation:', buf2.toString('base64'));

// 2. Modifying buffer content
const mutableBuf = Buffer.alloc(5);
mutableBuf.write('Node');
console.log('Written Buffer:', mutableBuf.toString('utf-8'));

// 3. Concatenating Buffers
const part1 = Buffer.from('Kodersolution ');
const part2 = Buffer.from('Developer Portal');
const combinedBuffer = Buffer.concat([part1, part2]);

console.log('Combined Output:', combinedBuffer.toString('utf-8'));

// 4. Slicing Buffers (shares underlying memory array)
const sliceBuf = combinedBuffer.subarray(0, 13);
console.log('Subarray Slice:', sliceBuf.toString('utf-8')); // 'Kodersolution'

Best Practices & Gotchas

  • Use Buffer.alloc() Over Buffer.allocUnsafe(): Buffer.alloc() initializes memory with zero filled bytes; allocUnsafe() is faster but leaves uninitialized memory containing sensitive past data.
  • Buffer.byteLength() vs .length: Use Buffer.byteLength(str) to calculate string byte size accurately, because multi-byte UTF-8 characters (like emojis) take more bytes than string character length.
  • Memory Sharing in subarray(): buf.subarray() returns a view over the original buffer without copying bytes; mutating the slice alters original buffer data.

Self-Check Challenge

Convert the ASCII text string "Node.js Security" into a hexadecimal string using Node.js Buffer.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum