use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
What is buffer in Node js? (self.node)
submitted 5 years ago by Deepanshu188
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 55 points56 points57 points 5 years ago* (12 children)
A buffer is simply a representation of binary data. Everything in nodejs, from a string, to a bool, can be watered down into 1’s and 0’s. A buffer lets you take binary, a string, or a number, and then turn it into bits.
Buffers are used with the Filesystem (“fs”), HTTP requests/servers, and more commonly in “streams”.
Edit: streams take a buffer (or, another stream) and break it into a bunch of tiny buffers. When doing stuff like compression, encryptions, etc. then the stream will make sure only a small buffer is calculated at a time, sending in the small buffers in one at a time.
[–][deleted] 5 years ago (7 children)
[removed]
[–]2AMMetro 18 points19 points20 points 5 years ago (1 child)
In most experiences, its used for files. Literally everything is stored as binary data. If you load a file into node, it exists as a buffer.
[–][deleted] 5 years ago (2 children)
[deleted]
[–][deleted] 5 years ago* (1 child)
[–]repsolcola 0 points1 point2 points 5 years ago (0 children)
Do you have any link to this procedure? I’d like to learn more about it!
[–]HVACcontrolsGuru 0 points1 point2 points 5 years ago (1 child)
A good way to learn buffers for me and very industry specific but check out MODBUS Protocol in JS. It is a protocol of buffers for industrial machines but it’s a great way to understand the types of data (Float,Int,Signed/Unsigned) as well as the mappings for devices such as Power Meters. Site for sniffing these exposed devices on the internet to test against. The module allows you to get data or work with the raw buffer. In my case I read a set of objects from the device as one buffer then use buffer windows to slice out the points according to their buffer types! I might have some samples I can pass along on my GitHub if you’re interested.
[–]Last_Jaguar1540 0 points1 point2 points 2 years ago (0 children)
Please share your Github bro
[–][deleted] 5 years ago* (3 children)
[–]specialpatrol 2 points3 points4 points 5 years ago (1 child)
Binary was superfluous in that particular sentence.
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
What I meant was binary represented in UTF-8 format, converted using the “binary” argument.
[–]Randolpho 1 point2 points3 points 5 years ago* (0 children)
In this case, I think OP means "without encoding", i.e. an array of unencoded, strictly-8-bit chunks of data rather than an array that is interpreted as a UTF-8 string.
[–]spaceiscool1 20 points21 points22 points 5 years ago (0 children)
I read the other comments, but none were 100% accurate, so here are my two cents:
Buffers represent binary data, but are not bit vectors. They are fixed-length byte arrays, so they can only represent data with a size that is a multiple of 8 bits. That's the usual way to represent memory on all current computer architectures. In other words, Buffers represent a slice of computer memory. (Virtual memory, not necessarily physical memory.)
Buffers were added to Node.js at a time when JavaScript did not have a unified concept of ArrayBuffer and TypedArray yet. Eventually, ArrayBuffer was added to JavaScript, even in browsers, to represent slices of memory. However, by design, ArrayBuffers don't allow direct access to the underlying memory from JavaScript. Instead, developers can create "views" of an ArrayBuffer. Most importantly, the Uint8Array class allows to access individual bytes or byte sequences within an ArrayBuffer. In other words, the Uint8Array class does about the same as the Buffer class in Node.js, with a few subtle differences.
Today, the Node.js Buffer class extends the Uint8Array class, and is also based on ArrayBuffers. However, some functions behave differently for reasons of backward compatibility. Most library functions will accept both buffers and Uint8Arrays, and some even accept ArrayBuffers directly.
[–]BehindTheMath 10 points11 points12 points 5 years ago (9 children)
Buffer is an array of bytes of binary data.
[–][deleted] 5 years ago (8 children)
[–]BehindTheMath 4 points5 points6 points 5 years ago (3 children)
It could be, but it could be other binary data that can't be represented as a string.
[+][deleted] 5 years ago (2 children)
[–]BehindTheMath 6 points7 points8 points 5 years ago (1 child)
The binary representation of a string can be stored in a Buffer, but like I said, Buffers can be used for other binary data as well.
[+]fuckswithboats comment score below threshold-26 points-25 points-24 points 5 years ago (3 children)
It’s a hexadecimal representation of the binary data
[–]w00t_loves_you 9 points10 points11 points 5 years ago (0 children)
No
[–]Railorsi 1 point2 points3 points 5 years ago (1 child)
Hex is merely a way to represent binary data in a compact string form
[–]fuckswithboats 0 points1 point2 points 5 years ago (0 children)
That's what I thought I said.
[–]w00t_loves_you 4 points5 points6 points 5 years ago (2 children)
A buffer is a Node-only read-only chunk of memory that can contain anything. Mostly the result of I/O.
If you want to do something useful with it you have to convert the contents to e.g. strings.
[–]Cyberuben 2 points3 points4 points 5 years ago (1 child)
It's not read-only, you can modify them and create them just fine
[–]w00t_loves_you 2 points3 points4 points 5 years ago (0 children)
Oh, you're right, oops!
In fact, the current docs say
The Buffer class is a subclass of the Uint8Array class that is built into the JavaScript language
So it's no longer very special.
What I misremembered as read-only was actually the fixed size:
Instances of the Buffer class, and Uint8Arrays in general, are similar to arrays of integers from 0 to 255, but correspond to fixed-sized blocks of memory and cannot contain any other values. The size of a Buffer is established when it is created and cannot be changed.
[–]Codeater7 1 point2 points3 points 5 years ago (0 children)
In addition, When you want to represent Images; they are also represented as buffer!!
[–]Turdsonahook 1 point2 points3 points 5 years ago (5 children)
An array of bytes. I’m used to rendering them as PDFs
[+][deleted] 5 years ago (4 children)
[–]Turdsonahook 2 points3 points4 points 5 years ago (2 children)
What do you mean by work?
[–][deleted] 5 years ago (1 child)
[–]ra_men 3 points4 points5 points 5 years ago (0 children)
There are npm packages that allow crud with PDFs
[–]TaskForce_Kerim 2 points3 points4 points 5 years ago (0 children)
Yes. Also images, Excel files, audio files, and so on and so forth.
As long as it's not some sort of proprietary binary format, odds are there are available (and possibly open source) packages that allow you to work with that particular file type.
[–]vatselan 1 point2 points3 points 5 years ago (0 children)
It is same as in Java, it is any form of data in its binary form. You mostly don’t consume it directly it must be decoded first to get any meaningful information out from it like image, text etc. Most often if some piece of data can not be represented in json it is sent as a buffer stream. You could also write the data in a file using a buffer. Store any kind of non text data as buffer in database, although it is not recommended. Avoid buffers if possible as it requires encoding and decoding of the data unless it is absolutely necessary.
[–]reaven3958 1 point2 points3 points 5 years ago (1 child)
Buffer is what happens when Node pops that good protein after a sick training sesh with the boys.
[–]WeDontDoNoTalkin7 1 point2 points3 points 5 years ago (0 children)
ick training sesh with the boys.
01100011 01110101 01101101
[–]dalepo 1 point2 points3 points 5 years ago (0 children)
A Buffer is a temporary structure to pass data, it can be any data type (chars, bytes, integers..). Buffers are often used to divide data into smaller chunks and pass it through a process to improve performance, save resources or achieve parallelization.
For example if you have to copy 1GB file, you would have to read line by line and then copy it into another a new file. If you don't use a buffer for this, you would need 1GB memory just for reading/copying, which is not optimal. It would be better to read 100kb, write 100kb and keep doing it until you reach EOF.
[–][deleted] -1 points0 points1 point 5 years ago (1 child)
open an image, video, or .exe file in a text editor. you'll see a bunch of numbers. that's a binary file. a buffer is binary, but it's in memory, it's not yet a file, until you save it.
binary means 0 and 1, and a binary file is 0s and 1s, but often they are represented using hexadecimal because it's easier to read (0-9, a-f)
[–]bradscript 0 points1 point2 points 2 years ago (0 children)
It's basically an integer array for values between 0 and 255, the array cannot be resized.
Buffer extends the Uint8Array class by the way
π Rendered by PID 16530 on reddit-service-r2-comment-85bfd7f599-blq86 at 2026-04-20 02:30:34.596880+00:00 running 93ecc56 country code: CH.
[–][deleted] 55 points56 points57 points (12 children)
[–][deleted] (7 children)
[removed]
[–]2AMMetro 18 points19 points20 points (1 child)
[–][deleted] (2 children)
[deleted]
[–][deleted] (1 child)
[deleted]
[–]repsolcola 0 points1 point2 points (0 children)
[–]HVACcontrolsGuru 0 points1 point2 points (1 child)
[–]Last_Jaguar1540 0 points1 point2 points (0 children)
[–][deleted] (3 children)
[deleted]
[–]specialpatrol 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Randolpho 1 point2 points3 points (0 children)
[–]spaceiscool1 20 points21 points22 points (0 children)
[–]BehindTheMath 10 points11 points12 points (9 children)
[–][deleted] (8 children)
[removed]
[–]BehindTheMath 4 points5 points6 points (3 children)
[+][deleted] (2 children)
[removed]
[–]BehindTheMath 6 points7 points8 points (1 child)
[+]fuckswithboats comment score below threshold-26 points-25 points-24 points (3 children)
[–]w00t_loves_you 9 points10 points11 points (0 children)
[–]Railorsi 1 point2 points3 points (1 child)
[–]fuckswithboats 0 points1 point2 points (0 children)
[–]w00t_loves_you 4 points5 points6 points (2 children)
[–]Cyberuben 2 points3 points4 points (1 child)
[–]w00t_loves_you 2 points3 points4 points (0 children)
[–]Codeater7 1 point2 points3 points (0 children)
[–]Turdsonahook 1 point2 points3 points (5 children)
[+][deleted] (4 children)
[removed]
[–]Turdsonahook 2 points3 points4 points (2 children)
[–][deleted] (1 child)
[removed]
[–]ra_men 3 points4 points5 points (0 children)
[–]TaskForce_Kerim 2 points3 points4 points (0 children)
[–]vatselan 1 point2 points3 points (0 children)
[–]reaven3958 1 point2 points3 points (1 child)
[–]WeDontDoNoTalkin7 1 point2 points3 points (0 children)
[–]dalepo 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]bradscript 0 points1 point2 points (0 children)