Buffer

Understand the workings of the Buffer class in Node.js.

What is a buffer?

Buffers in the context of computers refer to a temporary data storage that is used when moving data. Moving data can mean a lot of things. The most common, nowadays, would be video streaming. Depending on how fast your internet is, your video quality can change and so can your viewing experience. Video data is fetched from the server, stored in the buffer, processed, and sent to your display. This is why when you click on a video, you do not wait for it to download completely; instead, it starts playing right away. As more data is fetched, the buffer gets filled with newer data and old data is usually overwritten.

Buffers can help in synchronous operations as well, since the program does not have to wait for the entire file to be read from a local or network resource. Instead, as soon as some of the data arrives, the program can start processing it or move on to process more code.

Buffer in Node.js

Node.js has a Buffer class that provides us with the functionality we discussed above. This Buffer class is an array of bytes that is used to represent binary data. Streams, and other file system operations, are usually carried out with binary data, making buffers the ideal candidates for them.

The Buffer class is based on JavaScript’s Uint8Array. To put it simply, we can think of Buffer objects as arrays that only contain integers from 0 to 255. One distinction is that Buffer objects correspond to fixed-sized blocks of memory, which cannot be changed after they are created. There is no explicit way of deleting a buffer, but setting it to null will do the job. The memory will be handled by the garbage collectora garbage collector monitors allocated memory and determines when a block of allocated memory is no longer needed and reclaims it.

Buffer methods

The Console class needs to be imported but the Buffer class is in the global definition, so we do not need to import anything. Let’s take a look at a few methods that the Buffer class provides.

Press + to interact
const buf1 = Buffer.alloc(10);
console.log(buf1);
const buf2 = Buffer.alloc(5, 15);
console.log(buf2);
const buf3 = Buffer.allocUnsafe(10);
console.log(buf3);
buf3.fill(1);
console.log(buf3);
buf2.write("abcedf");
console.log(buf2);
const buf4 = Buffer.from([265, 6.5, -255, '7']);
console.log(buf4);
const buf5 = Buffer.from('Hello world');
console.log(buf5);
console.log(buf5.toString());

While we did state that buffers only store data as integers, when we output a buffer, it is represented in hexadecimal notation. This makes it easier and shorter to read.

  • To create a buffer, we use the Buffer.alloc method. It is passed an integer that will be the length of the buffer. This can be seen in line 1 of our code above. We can also pass it a second argument, which will be the fill value for the Buffer. By default, a Buffer created using the Buffer.alloc method is filled with 0. We can see this in line 4 of our code above.
  • Another way of creating a Buffer is by using the Buffer.allocUnsafe method, as it is done in line 7 of the code. This, as the name suggests, is unsafe as it returns a Buffer that may or may not be empty. However, this is faster than the safe method.
  • To fill or write to a buffer, we have two methods called fill and write that do exactly this. The fill method fills the Buffer with the argument it is passed. The write method writes a string to the Buffer. However, if the string passed cannot fit in the buffer, only part of it is written. These methods can be seen in line 10 and line 13, respectively.
  • Another popular method is the from method. It takes whatever it is passed and creates a Buffer from it. It does, however, convert the elements to binary by performing an &255 operation. This ensures that only integers from 0 to 255 are stored in the buffer. An example of this can be seen on line 16.
  • The from method can also be passed a string, but as we can see from the previous console.log outputs, we are presented with hexadecimal values. To get a different output, we can use the toString method to convert our Buffer to a string. See how this works on line 22 of the code above.

The Buffer class and its methods are very useful; however, being a low-level API, we rarely use it directly.

Get hands-on with 1300+ tech skills courses.