0
0
NodejsComparisonBeginner · 4 min read

Buffer vs String in Node.js: Key Differences and Usage

Buffer in Node.js is used to handle raw binary data efficiently, while string is for text data. Buffers are mutable and store bytes, whereas strings are immutable sequences of characters encoded in UTF-16 internally and usually UTF-8 externally.
⚖️

Quick Comparison

This table summarizes the main differences between Buffer and string in Node.js.

FactorBufferString
Data TypeRaw binary data (bytes)Text data (characters)
MutabilityMutable (can change content)Immutable (cannot change content)
EncodingStores raw bytes, encoding specified when convertingStored as UTF-16 internally, usually UTF-8 externally
Use CaseFile I/O, network data, binary protocolsText processing, display, manipulation
PerformanceFaster for binary data and large buffersSimpler for text but slower for binary operations
SizeFixed size allocatedVariable size, depends on characters
⚖️

Key Differences

Buffer is a special object in Node.js designed to handle raw binary data directly. It stores data as a sequence of bytes and allows you to manipulate these bytes efficiently. This makes it ideal for tasks like reading files, handling network packets, or working with binary protocols.

On the other hand, string is a sequence of characters used to represent text. Strings in JavaScript are immutable, meaning once created, their content cannot be changed. They are encoded in UTF-16 internally, but Node.js usually treats them as UTF-8 when interacting with external systems.

Buffers require explicit encoding when converting to and from strings, while strings are easier to work with for text but not suitable for binary data. Also, buffers have a fixed size allocated at creation, whereas strings can vary in length dynamically.

⚖️

Code Comparison

Here is how you create and manipulate a Buffer to store and display binary data.

javascript
const buf = Buffer.from('Hello, Node.js!');
console.log(buf);
console.log(buf.toString('utf8'));

// Modify buffer content
buf[0] = 72; // ASCII for 'H'
console.log(buf.toString('utf8'));
Output
<Buffer 48 65 6c 6c 6f 2c 20 4e 6f 64 65 2e 6a 73 21> Hello, Node.js! Hello, Node.js!
↔️

String Equivalent

Here is how you create and manipulate a string for the same text data.

javascript
let str = 'Hello, Node.js!';
console.log(str);

// Strings are immutable, so to change content you create a new string
str = 'H' + str.slice(1);
console.log(str);
Output
Hello, Node.js! Hello, Node.js!
🎯

When to Use Which

Choose Buffer when you need to work with raw binary data, such as reading files, handling network streams, or processing binary protocols. Buffers give you control over bytes and better performance for these tasks.

Choose string when working with text data that you want to display, manipulate, or process as characters. Strings are simpler and more natural for text but not suitable for binary data.

Key Takeaways

Use Buffer for raw binary data and efficient byte-level operations.
Use string for text data and character manipulation.
Buffers are mutable and fixed size; strings are immutable and variable length.
Always specify encoding when converting between buffers and strings.
Choose the type based on whether your data is binary or textual.