Buffers


1 | What is a Buffer?


A Buffer is a container of data in which data is stored in the binary format. A Buffer object is an instance of the Buffer class.


More specifically, a Buffer is a fixed-length sequence of bytes, i.e. a fixed-length sequence of integers from 0 to 255. A buffer is similar to a fixed-length array of bytes. In JavaScript, bytes are represented by the Uint8Array class. The Buffer class is a subclass of the Uint8Array class.


What differentiates a Buffer object from a Unint8Array object is the fact that Buffers can be directly created from and converted to Strings. More specifically, a byte from a Buffer can be created from and converted to a String character by specifying the String format to use. The Buffer class provides methods for converting Buffers into Strings and Strings to Buffers.


Last but not least, Buffers are iterable objects, therefore it is possible to use the statement for...of with Buffers.


In Node.js, most of the time, Buffers are simply the low-level representation of text. When we do not have to deal with binary data, we just have to make sure to convert Buffers into Strings, usually by using the default 'utf-8' String format.


5.2 | String to Buffer: Buffer.from()


It is possible to convert a String to a Buffer by calling the Buffer.from() static method from the Buffer class.


// 1. String to Buffer using implicit UTF-8
const string_1 = 'abc';

const buffer_1 = Buffer.from(string_1);
console.log(buffer_1); // <Buffer 61 62 63>

Remark 1


When converting between Buffers and Strings, a character format may be specified. If no character format is specified, UTF-8 will be used as the default ('utf-8'). The previous code is equivalent to:


// 2. String to Buffer using explicit UTF-8
const string_2 = 'abc';

const buffer_2 = Buffer.from(string_1, 'utf-8');
console.log(buffer_2); // <Buffer 61 62 63>

Remark 2


Converting a Buffer into a String can be called either decoding or encoding depending on the context, so do not try to remember if you want to encode or decode.


Remark 3

Apart from the 'utf-8' format, here are a few of the other formats available (we do not go into details):

  • 'utf16le' or 'ucs2'.
  • 'latin1' or binary.
  • 'base64'.
  • 'base64url'.
  • 'hex'.
  • 'ascii'.

5.3 | Buffer to String: buffer.prototype.toString()


The Buffer class does not have a constructor function that lets us create Buffers directly from bytes. We have to create a Buffer from a String first.


It is possible to convert a Buffer (back) into a String by calling the Buffer.prototype.toString() (referred as buf.toString() in the Node.js documentation).


// 3. Buffer to String
const buffer_3 = Buffer.from('def');

const string_3 = buffer_3.toString();
console.log(string_3); // 'def'

Author:
Initial publication date:
Last updated: