Understand Node.js Buffers

Share this video with your friends

Send Tweet

In this lesson, we cover the Node.js Buffer object in detail. Not only will you learn that the buffer object is a reference to a memory space outside of the V8 engine, but you will learn practical methods to access it, modify it, and convert it to standard Javascript objects that can be used by your code. Examples and discussion are provided for using the toString() method, determining the byte length of the buffer, writing to a buffer, how to avoid truncating data, comparing buffers for equality using both compare() and equals(), and copying buffers using slice().

Tiago Roldão
Tiago Roldão
~ 9 years ago

There is a slightly confusing description of Buffer.compare in this video.

Namely, mentioning return values of 1 as meaning "not equal", which, while true, ignores what compare actually does.

Will Button
Will Button(instructor)
~ 9 years ago

Good point. There is an opportunity there to explore compare further and how it is commonly used for sorting arrays.

Andrew
Andrew
~ 9 years ago

Since .slice is just a pointer, how would one go about actually creating a new buffer from a slice rather than just a pointer?

Will Button
Will Button(instructor)
~ 9 years ago

Create your new buffer, then use buffer.copy to copy the desired contents into it. See an example here: https://nodejs.org/api/buffer.html#buffer_buf_copy_targetbuffer_targetstart_sourcestart_sourceend

Leanne
Leanne
~ 8 years ago

With compare, I get a negative 1 (-1) when not equal and a zero when equal so I think there is a mistake here? Makes sense that it would work like other JS checking methods like indexOf

Will Button
Will Button(instructor)
~ 8 years ago

Yup- I didn't do a great job of explaining it in the video. The return value of compare indicates whether the buffer being compared is before, the same, or after the target buffer in the sort order. 0 is returned if it is the same, 1 if the target comes before the buffer being compared when sorted, -1 if it the target should come after the buffer being compared The current node.js documentation has been much improved since the recording of this lesson. Take a look and see if it helps, and thanks for posting! https://nodejs.org/api/buffer.html#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend

ryan
ryan
~ 8 years ago

Anyone else name their Buffer "const vampireSlayer = new Buffer(5);"?

Will Button
Will Button(instructor)
~ 8 years ago

Only in production. This ensures the app will only die if a wooden stake is driven through the event loop.

Daniel Otto
Daniel Otto
~ 8 years ago

what are the real-world uses for Buffer?

Will Button
Will Button(instructor)
~ 8 years ago

Retrieving/downloading/reading files.

Will Button
Will Button(instructor)
~ 8 years ago

More detailed answer: Using the AWS SDK for Javascript, you can download files from S3:

var s3 = new AWS.S3(config);
    var secure_file = s3.getObject({
        Bucket: 'my_bucket',
        Key: 'my_file.mp3'
    }, function(err, data){
        console.log('Accessing secure, encrypted file in S3');
        if (err) console.log(err);
        else console.log(data);
    })

Looking at the console output for data, you can see that the file object returned is a buffer:

{ AcceptRanges: 'bytes',
  LastModified: 2017-03-29T00:14:08.000Z,
  ContentLength: 46932816,
  ETag: '"624062b6df66799b753bbbf2285d02a8"',
  VersionId: 'null',
  ContentType: 'audio/mp3',
  Metadata: {},
  Body: <Buffer ff fb 94 64 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 58 69 6e 67 00 00 00
0f 00 01 4a 33 02 cc ... > }

Hope that helps!