Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
What is spawn in Node JS ?
Next article icon

What is Buffer in Node.js ?

Last Updated : 16 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Node, Buffer is used to store and manage binary data. Pure JavaScript is great with Unicode-encoded strings, but it does not handle binary data very well. It is not problematic when we perform an operation on data at the browser level but at the time of dealing with TCP stream and performing a read-write operation on the file system is required to deal with pure binary data.

To satisfy this need Node.js uses Buffer to handle the binary data. So in this article, we are going to know about buffer in Node.js.

What is Buffer in Node?

Buffer in Node is a built-in object used to perform operations on raw binary data. The buffer class allows us to handle the binary data directly.

Syntax:

const buf = Buffer.alloc(10); // Allocates a buffer of 10 bytes.

Generally, Buffer refers to the particular memory location in memory. Buffer and array have some similarities, but the difference is array can be any type, and it can be resizable. Buffers only deal with binary data, and it can not be resizable. Each integer in a buffer represents a byte. console.log() function is used to print the Buffer instance.

Buffer Methods:

NoMethodDescription
1Buffer.alloc(size)It creates a buffer and allocates size to it.
2Buffer.from(initialization)It initializes the buffer with given data.
3Buffer.write(data)It writes the data on the buffer.
4toString()It read data from the buffer and returned it.
5Buffer.isBuffer(object)It checks whether the object is a buffer or not.
6Buffer.lengthIt returns the length of the buffer.
7Buffer.copy(buffer,subsection size)It copies data from one buffer to another.
8Buffer.slice(start, end=buffer.length)It returns the subsection of data stored in a buffer.
9Buffer.concat([buffer,buffer])It concatenates two buffers.

Example: Basic Implementation of Node.js Buffers

Node
// Filename: index.js  // Different Method to create Buffer const buffer1 = Buffer.alloc(100); const buffer2 = new Buffer('GFG'); const buffer3 = Buffer.from([1, 2, 3, 4]);  // Writing data to Buffer buffer1.write("Happy Learning");  // Reading data from Buffer const a = buffer1.toString('utf-8'); console.log(a);  // Check object is buffer or not console.log(Buffer.isBuffer(buffer1));  // Check length of Buffer console.log(buffer1.length);  // Copy buffer const bufferSrc = new Buffer('ABC'); const bufferDest = Buffer.alloc(3); bufferSrc.copy(bufferDest);  const Data = bufferDest.toString('utf-8'); console.log(Data);  // Slicing data const bufferOld = new Buffer('GeeksForGeeks'); const bufferNew = bufferOld.slice(0, 4); console.log(bufferNew.toString());  // concatenate two buffer const bufferOne = new Buffer('Happy Learning '); const bufferTwo = new Buffer('With GFG'); const bufferThree = Buffer.concat([bufferOne, bufferTwo]); console.log(bufferThree.toString()); 

Run the index.js file using the following command:

node index.js

Output:

Happy Learning
true
100
ABC
Geek
Happy Learning With GFG

Buffers are highly efficient when dealing with large amounts of binary data or when performance is critical, as they bypass the overhead of encoding/decoding data into JavaScript's native string format.

Reference: https://nodejs.org/api/buffer.html


Next Article
What is spawn in Node JS ?
author
abhijitmahajan772
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Node.js-Methods
  • NodeJS-Questions

Similar Reads

  • What are Buffers in Node.js ?
    Buffers are an essential concept in Node.js, especially when working with binary data streams such as files, network protocols, or image processing. Unlike JavaScript, which is typically used to handle text-based data, Node.js provides buffers to manage raw binary data. This article delves into what
    4 min read
  • What is Chunk in Node.js ?
    In Node.js, the term "chunk" refers to a small, manageable piece of data that is part of a larger dataset. Node.js processes data in chunks, especially when dealing with streams, which makes handling large files or data sources more efficient and memory-friendly. This article explores what chunks ar
    4 min read
  • Node.js Buffers
    Node.js Buffers are used to handle binary data directly in memory. They provide a way to work with raw binary data streams efficiently, crucial for I/O operations, such as reading from files or receiving data over a network. Buffers in Node.jsBuffers are instances of the Buffer class in Node.js. Buf
    4 min read
  • What is Clustering in Node?
    Clustering in Node refers to a technique used to enhance the performance and scalability of NodeJS applications by utilizing the capabilities of multi-core systems. With clustering, you can create multiple instances of the NodeJS process, known as workers, each running on a separate CPU core. By dis
    2 min read
  • What is spawn in Node JS ?
    Node JS is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of an internet browser. In this article, we will learn about the Spawn in NodeJs. PrerequisitesNodeJS fundamentalsAsynchronous ProgrammingChild ProcessesSpawn i
    3 min read
  • What is Piping in Node.js ?
    Piping in NodeJS is the process by which byte data from one stream is sent to another stream. It is a powerful feature in Node.js that allows data to be transferred from one stream to another seamlessly. Streams are an integral part of Node.js, providing a mechanism for handling data that is too lar
    6 min read
  • What are Modules in Node.js ?
    In Node.js Application, a Module can be considered as a block of code that provide a simple or complex functionality that can communicate with external application. Modules can be organized in a single file or a collection of multiple files/folders. Almost all programmers prefer modules because of t
    5 min read
  • What is File System Module in Node.js ?
    Node.js is a powerful platform for building server-side applications, and one of its key features is its ability to interact with the file system. The File System (fs) module in Node.js provides an extensive API to work with files and directories, allowing developers to perform operations such as re
    3 min read
  • Node.js Buffer.entries() Method
    The Buffer.entries() method is used to create and return the iterator of [index, byte] pairs from the contents of the buffer.  Syntax: Buffer.entries() Parameters: This method does not accept any parameters.  Return value: This method returns an Iterator object of pair in [index, byte] format.  Exam
    2 min read
  • Node.js Buffer.includes() Method
    Buffer is a temporary memory storage which stores the data when it is being moved from one place to another. It is like an array of integers. The Buffer.includes() method checks whether the provided value is present or included in the buffer or not. Syntax: buffer.includes( value, byteOffset, encodi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences