1

I understand why I need to use Buffer.

But I'm not super clear about the actual use of buffer syntax.

in the example below,

       const parsedBody = Buffer.concat(body).toString();
        const message = parsedBody.split('=')[1];
        fs.writeFileSync('receivedText', message);

buffer was used to make a delay before

    const message = parsedBody.split('=')[1];
    fs.writeFileSync('receivedText', message);

is excuted.

but syntax-wise it's bit strage for me.

What if I want to make an array of many things, like

let arr=[]
for (let i=0; i<10; i++){
arr.push('hi')
}

I'm pretty sure that I can't do simply like

Buffer.(
let arr=[]
for (let i=0; i<10; i++){
arr.push('hi')
}
)

It feels like an async function and await combination though, I believe there must be something behind the scene.

As a simple example answer for that, What should I do if I want to do for loop using Buffer?

Thank you in advance.

5
  • 4
    Buffer is an object used for holding binary data. I can't tell what your question is about because it doesn't appear to have anything to do with the expected use of a Buffer. Commented Dec 19, 2020 at 4:52
  • @jfriend00, You meant it doesn't have anything to do with delaying things? like 'await?' Commented Dec 19, 2020 at 14:32
  • 3
    No. It doesn't have anything to do with delaying things. It has to do with storing things that normally can't be stored in ordinary strings like the byte 0x00 etc. Buffer is needed to handle things that are not strings like JPEG images, MP4 videos etc and storing them in variables. Commented Dec 19, 2020 at 14:41
  • 1
    As such there is nothing magical about Buffer. It is just an object type like strings, arrays, DOMNode, HTMLElement, jQuery, Express etc. Commented Dec 19, 2020 at 14:43
  • 1
    Just like any object. There is no special syntax with Buffer. Instead just like other objects like Express or Promise or jQuery, the Buffer class have methods and properties that are specific to it Commented Dec 19, 2020 at 14:45

1 Answer 1

5

Buffer is an object in nodejs that is used for holding binary data - the type of data that can't be held in a UTF-8 string. This might be things like videos or images or other binary data.

It's very much like a Javascript-native Uint8Array, in fact, it is actually a Uint8Array under the covers now though this wasn't always the case because Buffer likely existed in nodejs before Uint8Array was available in Javascript.

Inside of the Buffer constructor is a call to Buffer.alloc() which in all cases ends up doing a new FastBuffer(...) and FastBuffer is declared as this:

class FastBuffer extends Uint8Array {}

So, a nodejs Buffer object is a sub-class of Uint8Array with some new methods added.

A Buffer has nothing at all do do with asynchronous operations except that some asynchronous operations such as fs.readFile() produce a Buffer object as their output, but a Buffer itself has nothing at all to do with the timing of asynchronous operations.

Think of a Buffer like a special type of array that gives you byte level access to individual bytes of data, but instead of an array element which can hold any type of data, an individual element in a Buffer holds only a byte of data and thus the whole Buffer object holds something like an array of bytes of data.

You would use a Buffer object either when an API you call produces one or when you need to process data at the byte level or with binary values.

It feels like an async function and await combination though, I believe there must be something behind the scene.

Somewhere along the line, you got confused by something you saw. A buffer has nothing to do with async or await. It's just an object type that you can create yourself or you can use when some API returns one. It has a set of methods and properties as described here in the doc.

Sign up to request clarification or add additional context in comments.

5 Comments

Another important distinction from strings: buffers are mutable. They're much more similar to arrays. Also I guess buffers should be treated as more or less equivalent to Uint8Arrays, it's only due history (predating typed arrays) that they exist at all.
@Bergi - I updated the answer to incorporate your thoughts.
I thought all data in a computer was stored in binary. So I don't get what would make a Buffer different from any other variable
@JuanPerez - A Buffer is a series of bytes that you can access one byte at a time (or you can read/interpret a given set of 1, 2, 4 or 8 bytes at a time). The data is untyped and just raw binary. When you access a byte, it will just be a number 0-255. Making any sense out of it is up to your code and your code must know how to interpret it. For example, if you want to convert a byte to a printable or displayable character, you have to manually convert its numeric value yourself into a string variable so you can use it as a string character in Javascript.
@JuanPerez - Or, if you want to interpret four consecutive bytes as a little endian dword value, you have to call the right conversion functions on those four bytes to convert it to the appropriate number using the little endian dword format. A Javascript variable knows what type of data is in it (such as Number or String) and you can automatically use it as that type without having to do manual conversions.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.