Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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