Changes to Buffers in Node.js 6

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

In versions of nodejs prior to 6, how you created a buffer and what you did with it produced different behaviors. This made it possible to introduce security or reliability issues into your code. At the time however, JavaScript had no means of reading or manipulating streams of binary data. Now that TypedArray has been added in ES6, the Buffer class implements the Uint8Array API in a manner that better suits node.js use cases. In this lesson, you will learn what that means to your existing code and how to update your code to use the new Buffer APIs.

[00:00] Developers should migrate all existing uses of the new buffer constructor to one of the following new APIs. The buffer.from array returns a new buffer containing a copy of the provided octets.

[00:14] Calling buffer.from and supplying a buffer will return a new buffer that provides a copy of the one that was provided. We can create a new buffer called buff2 from buff1. It creates an exact copy of it, but each one has its own memory space.

[00:29] Take a look at this. If I log each buffer out, they both contain the same values. Now, I can make a change to buff1. If I iterate through each one again, buff1 has the updated value and buff2 still has the original values as when we created it.

[00:49] Using buffer.from and supplying a string returns a new buffer containing a copy of the provided string. I can return that with different character encodings.

[01:00] Buffer.from with an array buffer returns a new buffer that shares the same memory as the given array buffer. You saw earlier how I could create a new buffer by copying an existing buffer.

[01:11] It's also possible to create a buffer that shares the same memory as a typed array. Let's start by creating a typed array and assign some values to it. I'll first create a buffer that copies the memory. Then, one that shares the same memory by using the typed array's buffer property.

[01:28] If I print all three of those out, the results are the same. Now if I change the value in the original typed array, when I log all three out again, the typed array and buff5 with the shared memory are updated, but buff4 remains unchanged.

[01:46] This also illustrates that typed arrays and buffers are not identical. The buffer object's memory is interpreted as a distinct array of elements, not as a byte array of the target type.

[01:59] You can also allocate new buffers using buffer.alloc and specifying the size. Buffer.alloc unsafe returns a new buffer of the specified size whose content must be initialized using buff.fill or written to completely.

[02:16] What's the difference in these two? The name probably tells you a lot. When you create a new buffer with buff.alloc, the allocated memory is zeroed out on creation. With buffer.alloc unsafe, the allocated memory's not zeroed out.

[02:30] Creating a buffer with buffer.alloc unsafe is faster initially, but with the added cost that you're responsible for initializing the buffer to prevent accidentally exposing old and potentially sensitive data.

[02:45] Finally, buffers can be iterated over using the ECMAScript 2015 for of syntax. If I create a new buffer, I can then iterate over that using for of. You can also create iterators using the values, keys, and entries methods. We can do this to iterate over the values, or we could iterate over the keys or we can iterate over the entire entry...

egghead
egghead
~ a minute ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today