← Back to Node.js, page 1

nodejs-0012 · Easy fresher

What is a Buffer in Node.js, and how do encodings like utf8, hex and base64 work with it?

A Buffer is a fixed-size block of raw bytes, and in current Node it is a subclass of Uint8Array. Encodings define how text becomes bytes (Buffer.from(text, encoding)) and how bytes become text (buf.toString(encoding)). Byte length and string length are different for non-ASCII text.

Full answer

JavaScript strings are text. Files, sockets and hashes work with bytes. Buffer is the bridge: a fixed-length sequence of bytes, with methods for reading, writing, slicing and converting.

Since Node 4 a Buffer is a Uint8Array with extra methods, so it works with typed-array APIs and Web APIs.

An encoding is the rule that maps characters to bytes:

  • utf8 (the default): one to four bytes per character. English letters use one byte, Devanagari letters use three.
  • hex: two hex digits per byte, often used for hashes.
  • base64: three bytes become four characters. It is used in data URLs, JWT parts and emails.
  • latin1 and ascii: one byte per character, for legacy data.

Because of this, string.length counts UTF-16 code units, while Buffer.byteLength(string) counts bytes. They are equal only for plain ASCII.

Buffers are fixed size. You cannot push into them. To combine pieces, use Buffer.concat([a, b]). Compare with Buffer.compare or buf.equals, never with ==.

javascript
const text = 'नमस्ते';

const buf = Buffer.from(text, 'utf8');
console.log('string length:', text.length);
console.log('byte length:  ', buf.length, '(same as Buffer.byteLength:', Buffer.byteLength(text) + ')');
console.log('is a Uint8Array:', buf instanceof Uint8Array);

const hello = Buffer.from('Hello Schoolabe');
console.log('hex:   ', hello.toString('hex'));
console.log('base64:', hello.toString('base64'));
console.log('round trip:', Buffer.from(hello.toString('base64'), 'base64').toString());

console.log('equals:', Buffer.from('abc').equals(Buffer.from('abc')));
console.log('concat:', Buffer.concat([Buffer.from('foo'), Buffer.from('bar')]).toString());

The Hindi word has 6 UTF-16 code units and takes 18 bytes in UTF-8. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): string length: 6 byte length: 18 (same as Buffer.byteLength: 18) is a Uint8Array: true hex: 48656c6c6f205363686f6f6c616265 base64: SGVsbG8gU2Nob29sYWJl round trip: Hello Schoolabe equals: true concat: foobar

Key points

  • Buffer is raw bytes and a subclass of Uint8Array.
  • Encodings convert between text and bytes: utf8, hex, base64 and others.
  • String length and byte length differ for non-ASCII text.
  • Use Buffer.concat to join and buf.equals to compare.

Common mistakes

  • Assuming string.length equals the number of bytes.
  • Comparing buffers with == or ===, which compares references.
  • Using new Buffer(), which is deprecated and unsafe. Use Buffer.from or Buffer.alloc.
  • Calling toString() on a partial multi-byte chunk.

Follow-ups

  • How is Buffer different from ArrayBuffer?
  • What is base64url and where is it used?
  • How do you convert a Buffer to JSON?

Tips

  • Show the byte length vs string length example. It proves you understand encodings.
  • Say Buffer extends Uint8Array.
  • Mention that new Buffer() is deprecated.