Explorer
Node.js

Buffer and Binary Data

Handling Binary Data: The Buffer Class

Unlike browser-based JavaScript environments, Node.js was designed to interact intimately with the operating system, necessitating robust tools to read and write raw binary data originating from TCP streams, file systems, and cryptography routines.

The Role of Buffer

The Buffer class is a globally available utility that allocates fixed-size chunks of memory outside the V8 heap engine. This allows developers to interact with memory arrays containing raw bytes directly.

  • Memory Allocation: Use Buffer.alloc(size) for zero-filled, safe allocations. Buffer.allocUnsafe(size) is faster but risks leaking sensitive residual memory.
  • Character Encodings: Buffers effortlessly convert binary representations into readable formats like utf8, hex, or base64.

Finished this lesson?

Mark this chapter complete to update your learning streak and unlock the next lesson.