Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Binding Values

Duarte Monteiro
Duarte Monteiro
22,300 Points

What is a buffer?

I'm a little confused with this video, what is a buffer in the node.js documentation?

https://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options

"If the encoding option is specified then this function returns a string. Otherwise it returns a buffer."

3 Answers

From Stack Overflow: http://stackoverflow.com/questions/14551006/what-is-a-buffer-in-node-js

A Buffer is a chunk of memory, just like you would have it in C/C++. 
You can interpret this memory as an array of integer or floating point numbers of various lengths, or as a binary string. 
Unlike higher-level data structures like arrays, a buffer is not resizable.

It corresponds roughly to:

char* or char[] in C/C++
byte[] in Java
A mutable bytes or a non-resizable bytearray in Python
Strings in php if they were mutable

Hope that helps :)

Raw data is stored in instances of the Buffer class. A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.

Sathish kumar Balasubramanian
Sathish kumar Balasubramanian
6,748 Points

Buffers are instances of the Buffer class in node, which is designed to handle raw binary data. Each buffer corresponds to some raw memory allocated outside V8. Buffers act somewhat like arrays of integers, but aren't re-sizable and have a whole bunch of methods specifically for binary data. In addition, the "integers" in a buffer each represent a byte and so are limited to values from 0 to 255 (2^8 - 1), inclusive.

the above answer is a direct quote from this source:

https://docs.nodejitsu.com/articles/advanced/buffers/how-to-use-buffers