Course contentsShow
← JavaScript
Lesson 1591 of 2,72334. Streams and BuffersFree lesson

Buffer and TypedArrays

Relate buffers to Uint8Array and understand when to use each in different contexts.

Buffer and TypedArrays

What You'll Learn

You'll understand the relationship between Node.js Buffers and JavaScript TypedArrays, and when to use each in different contexts.

The Relationship Between Buffer and TypedArrays

Buffer is actually built on top of Uint8Array. When Node.js was created, JavaScript didn't have a native way to handle binary data. Node.js introduced Buffer to fill that gap. Later, JavaScript added TypedArrays (including Uint8Array) to the language standard. Node.js then updated Buffer to be a subclass of Uint8Array, giving you the best of both worlds.

Think of it like this: Uint8Array is the universal toolbox that works everywhere JavaScript runs, while Buffer is Node.js's enhanced version with extra conveniences.

Key Differences

Buffer is Node.js-specific and includes additional methods for common binary operations (like toString() with various encodings, readInt32LE(), etc.). It's optimized for file I/O, network operations, and working with strings in different encodings.

Uint8Array is standard JavaScript available in browsers and all JavaScript environments. It's part of the TypedArray family (Uint16Array, Int32Array, etc.) and provides a lower-level, platform-agnostic way to work with binary data.

When to Use Each

Use Buffer when:

  • You're in Node.js and need encoding/decoding (UTF-8, hex, base64)
  • Working with Node.js streams, file systems, or network protocols
  • You need Buffer-specific utility methods

Use Uint8Array (or other TypedArrays) when:

  • Writing code that must run in browsers and Node.js
  • Working with WebAssembly or Web APIs
  • You need precise control over byte representation (8-bit, 16-bit, 32-bit integers)

Key Takeaway: Buffer extends Uint8Array with Node.js-specific conveniences; use Buffer in Node.js for its extra features, and Uint8Array when you need cross-platform compatibility or specific integer types.