The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Understand Node.js Buffers

Understand Node.js Buffers

10:30
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().
Watch this lesson now
Avatar
egghead.io

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().

Avatar
Tiago Roldão

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.

Avatar
Will

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

In reply to Tiago Roldão
Avatar
Andrew

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

Avatar
Will

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

In reply to Andrew
Avatar
Leanne

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

In reply to Tiago Roldão
Avatar
Will

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

In reply to Leanne
Avatar
ryan

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

Avatar
Will

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

In reply to ryan
Avatar
Dhafir Moussa

Thank you for the very clear presentation.

In reply to egghead.io
Avatar
Will

My pleasure! Glad you found it helpful!

In reply to Dhafir Moussa
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?