42

As you can read in the Node.js documentation on the Buffer class, a buffer

is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.

So far, so good.

What now puzzles me is the question what a buffer is technically speaking. Is it an array with just some additional functions for creating and converting to strings using specific encodings?

Or is there "more" to it?

1

3 Answers 3

35

A Buffer is a chunk of memory, just like you would have it in C/C++. You can interpret this memory as an array of integer or floating point numbers of various lengths, or as a binary string. Unlike higher-level data structures like arrays, a buffer is not resizable.

It corresponds roughly to:

  • char* or char[] in C/C++
  • byte[] in Java
  • A mutable bytes or a non-resizable bytearray in Python
  • Strings in php if they were mutable
12
  • 1
    Okay, the comparison to a char[] or char* in C makes sense to me. Thanks for clarifying this! This analogy helps me to understand the meaning of buffers :-).
    – Golo Roden
    Jan 27, 2013 at 19:08
  • 1
    @phihag Could you give an example like where we supposed to use buffer over regular data type like array itself. Jun 3, 2019 at 10:24
  • 1
    @SachinBhandari Say you want to read a 1GB binary file. You can represent it as an array of integers or a Buffer. With a Buffer, you need roughly 1GB storage (+ on the order of 32 Bytes overhead). Getting an element from the buffer will be one boundary check and one memory access. ...
    – phihag
    Jun 3, 2019 at 11:06
  • 1
    @SachinBhandari ... If you were to use an array of integers between 0 and 255, your memory requirements would be 1G * pointer size. On a 64 Bit system, that works out to 8GB. There's also negligible overhead (the numbers 0-255 in memory, and the integer array), but unless you're careful also some non-negligible overhead, caused by overprovisioning of the array. Also, getting an element from the array will entail two memory accesses; one for the element and one for the item.
    – phihag
    Jun 3, 2019 at 11:07
  • 2
    @SachinBhandari node.js's Buffer predates ArrayBuffer. At the time, there was no ArrayBuffer, and that's why the Buffer class in node.js was created. Browser vendors did not like the Buffer interface, and created their own ArrayBuffer. The TypedArray family of classes provides different interpretations of the memory region of an ArrayBuffer. Nowadays, Buffer is just a Uint8Array (which itself is one of the TypedArray implementations).
    – phihag
    Jun 5, 2019 at 19:35
17

BUFFER is a temporary holding spot for data being moved from one place to another.

In order to understand what is Buffer, we need to know how a computer will process things. See the chart below.

The concept is like if you are watching a Youtube Video, you can start to watch a video without downloading the whole video. If your internet speed is too slow, you would see "buffering", that means the computer is trying to collect data in order for you to keep watching that video.

1
  • Somewhat understood. Can you specify a practical example(code) of this thing? Oct 7, 2017 at 16:45
7

Explanation from http://nodejitsu.com/...

Buffers are instances of the Buffer class in node, which is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't resizable and have a whole bunch of methods specifically for binary data. In addition, the "integers" in a buffer each represent a byte and so are limited to values from 0 to 255 (2^8 - 1), inclusive.

Read more: Buffers in Node.js

1
  • 2
    Yes, I read that in the documentation as well, but what does that actually mean? May I use a buffer just like an array of fixed size with the constraint that it only takes integers from 0 to 255 as values?
    – Golo Roden
    Jan 27, 2013 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.