Node.js: Code Outside the Browser / Streams and Buffers

What is Buffer?

πŸ“¦ What Is a Buffer?

A Buffer is a fixed-size chunk of memory for handling raw binary data β€” the kind of data you get from files, network requests, or streams, before it's been decoded into text.

Unlike a string, a buffer works directly with bytes, not characters. It's a global in Node.js β€” no require needed.

const buf = Buffer.from('hello');
console.log(buf);
// <Buffer 68 65 6c 6c 6f>
  • Each number in the output (68 65 6c 6c 6f) is one byte, shown in hexadecimal β€” the raw bytes that make up "hello" when encoded as UTF-8.

  • Buffers exist because JavaScript strings weren't built for binary data β€” things like image files or network packets need a byte-level representation.


πŸ—οΈ Creating Buffers

const fromString = Buffer.from('hello');           // from a string
const fromArray = Buffer.from([104, 101, 108, 108, 111]); // from byte values
const empty = Buffer.alloc(10);                     // 10 zeroed-out bytes
const fast = Buffer.allocUnsafe(10);                 // 10 bytes, uninitialized (faster, unsafe)
  • Buffer.from(string) builds a buffer from existing data β€” a string, an array of byte values, or another buffer.

  • Buffer.alloc(size) creates a buffer of a given size, filled with zeros β€” safe to use right away.

  • Buffer.allocUnsafe(size) creates a buffer faster, but skips zeroing it out β€” it may contain leftover data from memory. Only use it if you're about to overwrite every byte yourself.

⚠️ Avoid new Buffer() β€” it's deprecated. Buffer.from() and Buffer.alloc() replaced it because the old constructor's behavior changed depending on what type of argument you passed, which caused security bugs.


πŸ”€ Converting Between Buffers and Strings

const buf = Buffer.from('hello world');

console.log(buf.toString());        // 'hello world' (defaults to utf8)
console.log(buf.toString('hex'));   // '68656c6c6f20776f726c64'
console.log(buf.toString('base64')); // 'aGVsbG8gd29ybGQ='
  • .toString() decodes the raw bytes back into a readable string.

  • Passing an encoding ('hex', 'base64', etc.) controls the format of the output β€” useful when sending binary data somewhere that only accepts text, like a URL or JSON.


πŸ“ Reading Buffer Info

const buf = Buffer.from('hello');

console.log(buf.length);   // 5 β€” number of bytes
console.log(buf[0]);       // 104 β€” byte value at index 0
  • .length gives the number of bytes, not characters β€” for most simple text these match, but multi-byte characters (emoji, accented letters) take up more than one byte each.

  • Buffers support index access like arrays, but each index returns a raw byte number, not a character.


Next up: slicing, combining, writing, and comparing buffers β€” plus real-world examples of buffers in action.