0
0
Node.jsframework~5 mins

Buffer allocation and encoding in Node.js

Choose your learning style9 modes available
Introduction

Buffers let you work with raw binary data in Node.js. You use them to store and manipulate bytes directly.

When reading or writing files with binary data like images or videos.
When sending or receiving data over a network socket.
When converting strings to bytes for encoding or decoding.
When working with streams that handle raw data.
When you need to manipulate data at the byte level for performance.
Syntax
Node.js
const buf = Buffer.alloc(size, fill, encoding);
const bufFromString = Buffer.from(string, encoding);

Buffer.alloc(size) creates a buffer of fixed size filled with zeros by default.

Buffer.from() creates a buffer from a string or array, using the specified encoding.

Examples
Creates a buffer of 5 bytes, all set to zero.
Node.js
const buf = Buffer.alloc(5);
Creates a buffer of 5 bytes, each byte set to the ASCII code for 'a'.
Node.js
const buf = Buffer.alloc(5, 'a');
Creates a buffer from the string 'hello' using UTF-8 encoding.
Node.js
const buf = Buffer.from('hello', 'utf8');
Creates a buffer from an array of bytes.
Node.js
const buf = Buffer.from([1, 2, 3, 4]);
Sample Program

This program shows how to allocate buffers with zeros or a specific character, and how to create a buffer from a string. It prints the buffers to see their contents.

Node.js
import { Buffer } from 'node:buffer';

// Allocate a buffer of 10 bytes, filled with zeros
const zeroBuffer = Buffer.alloc(10);

// Allocate a buffer of 10 bytes, filled with 'x' (ASCII 120)
const xBuffer = Buffer.alloc(10, 'x');

// Create a buffer from a string with UTF-8 encoding
const helloBuffer = Buffer.from('hello', 'utf8');

// Show the buffers as strings and arrays
console.log('zeroBuffer:', zeroBuffer);
console.log('xBuffer as string:', xBuffer.toString());
console.log('helloBuffer as array:', helloBuffer);
console.log('helloBuffer as string:', helloBuffer.toString());
OutputSuccess
Important Notes

Buffers are fixed size. You cannot resize them after creation.

Always specify encoding when converting strings to buffers to avoid confusion.

Use Buffer.allocUnsafe(size) only if you plan to overwrite all bytes, as it may contain old data.

Summary

Buffers store raw binary data in Node.js.

Use Buffer.alloc() to create zero-filled buffers.

Use Buffer.from() to create buffers from strings or arrays with encoding.