VOOZH about

URL: https://www.geeksforgeeks.org/node-js/what-is-buffer-in-node-js/

⇱ Buffer in Node.js - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Buffer in Node.js

Last Updated : 15 Apr, 2026

Buffer in Node.js is a built-in object used to handle and manipulate raw binary data, especially when working with streams, files, or network operations.

• Represents raw memory allocation for binary data
• Used in low-level operations like file systems and networking
• Provides methods for reading and writing binary data

Syntax:

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

Characteristics of Buffer

Buffer is a fixed-size memory structure in Node.js designed specifically for handling raw binary data efficiently.

• A Buffer represents a fixed memory location used to store binary data.
• Unlike arrays, Buffers are not resizable and only handle binary data.
• Arrays can store any data type, but Buffers store only binary values.
• Each integer in a Buffer represents one byte.
• console.log() can be used to print the Buffer instance.

Buffer Methods

Buffer methods provide built-in functions to create, manipulate, and manage binary data efficiently in Node.js.

Method

Description

Buffer.alloc(size)

Creates a new buffer of specified size.

Buffer.from(data)

Creates a buffer from given data.

buffer.write(data)

Writes data to the buffer.

buffer.toString()

Converts buffer data to a string.

Buffer.isBuffer(obj)

Checks if the object is a buffer.

buffer.length

Returns the length of the buffer in bytes.

buffer.copy(targetBuffer)

Copies data from one buffer to another.

buffer.slice(start, end)

Returns a portion of the buffer.

Buffer.concat([buffers])

Concatenates multiple buffers.

Example: Basic Implementation of Node.js Buffers

Run the index.js file using the following command:

node index.js

Output:

Happy Learning
true
100
ABC
Geek
Happy Learning With GFG
  • Efficient for processing large binary data and performance-critical tasks.
  • Avoids the overhead of encoding and decoding data into JavaScript strings.
Comment

Explore