This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Changes to Buffers in Node.js 6

3:14 Node.js lesson by

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.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?