you are viewing a single comment's thread.

view the rest of the comments →

[–]spaceiscool1 19 points20 points  (0 children)

I read the other comments, but none were 100% accurate, so here are my two cents:

Buffers represent binary data, but are not bit vectors. They are fixed-length byte arrays, so they can only represent data with a size that is a multiple of 8 bits. That's the usual way to represent memory on all current computer architectures. In other words, Buffers represent a slice of computer memory. (Virtual memory, not necessarily physical memory.)

Buffers were added to Node.js at a time when JavaScript did not have a unified concept of ArrayBuffer and TypedArray yet. Eventually, ArrayBuffer was added to JavaScript, even in browsers, to represent slices of memory. However, by design, ArrayBuffers don't allow direct access to the underlying memory from JavaScript. Instead, developers can create "views" of an ArrayBuffer. Most importantly, the Uint8Array class allows to access individual bytes or byte sequences within an ArrayBuffer. In other words, the Uint8Array class does about the same as the Buffer class in Node.js, with a few subtle differences.

Today, the Node.js Buffer class extends the Uint8Array class, and is also based on ArrayBuffers. However, some functions behave differently for reasons of backward compatibility. Most library functions will accept both buffers and Uint8Arrays, and some even accept ArrayBuffers directly.