Uint8Array is a general-purpose byte-array that’s available in both nodejs and browsers. Buffer is a subclass of Uint8Array that’s only available in Node.js (for historical reasons). both are primarily used for manipulating binary (byte) data.

historically, when Node.js first came about, general-purpose Uint8Arrays didn’t exist, so it had to invent its own “Buffer” type for handling binary-data. after general-purpose Uint8Arrays were introduced with ES6, Node.js (after version 4.0) decided to migrate Buffer over from a separate data-type -> subclass of Uint8Array (to try and make it more browser-compatible with Uint8Array).

https://www.quora.com/What-is-the-relationship-between-a-Buffer-and-an-Uint8Array-in-Node-js

Answer from Orkhan Huseynli on Stack Overflow
🌐
Sindre Sorhus
sindresorhus.com › blog › goodbye-nodejs-buffer
Goodbye, Node.js Buffer — Sindre Sorhus
October 24, 2023 - The Buffer type has been the cornerstone for binary data handling in Node.js since the beginning. However, these days we have Uint8Array, which is a native JavaScript type and works cross-platform. While Buffer is an instance of Uint8Array, it introduces numerous methods that are not available ...
Discussions

body size difference using uint8array vs buffer for post request
Describe the issue i found that when using uint8array vs buffer for axios post request. the request body size seems different. it seems when sending uint8array buffer, it seems the size doubles. no... More on github.com
🌐 github.com
4
March 9, 2023
What is the difference between an array and an ArrayBuffer?
I'm just wondering why everyone uses ArrayBuffer instead of just a normal array, string or stringified JSON for sending messages from the server to the client. Is it more efficient? Also, just won... More on stackoverflow.com
🌐 stackoverflow.com
No differentiation between `ArrayBuffer` and `Uint8Array`
Bug Report TypeScript can not differentiate between ArrayBuffer and Uint8Array (or other typed array types). const t: ArrayBuffer = new Uint8Array(); 🔎 Search Terms Structural typing Brand checking Related #50714, #42534, #202 🙁 Actual b... More on github.com
🌐 github.com
3
July 15, 2023
Start moving to Uint8Array in new APIs?
There was a suggestion by @jasnell to use Uint8Arrays in new APIs over Buffers as well as a weigh-in by @sindresorhus saying it is easier to author cross-platform APIs when using Uint8Arrays. Here ... More on github.com
🌐 github.com
34
January 19, 2022
🌐
Reddit
reddit.com › r/node › eli5: file, uint8array, buffer, arraybuffer, bytesarray, blob…
r/node on Reddit: ELI5: File, Uint8Array, Buffer, Arraybuffer, BytesArray, Blob…
December 16, 2021 -

I’m always ending up guessing.. I need to somehow understand. I have no idea what I’m doing when working with files. Just trying stuff until it works. Someone plz ELI5..

My current issue is getting a png file from Firebase storage which gives a “file” which I’m trying to add on a pdf, where the library expects a Uint8Array or Arraybuffer. I’ve been doing similar things before. But end up just guessing my way until it works, and this time.. well it doesn’t work. I guessed it all with no success. I don’t know what I’m doing so I guess I need to actually learn ;)

Top answer
1 of 2
36
A digital system, like a computer works on digital data, thus things which can be represented as digits. Or simpler: Computer sees a bit of numbers and works on them. Also a text is just a long number. Some time ago, one decided to have computers work on binary digits and to group 8 out of those as the typical size. That is a byte. (There were computers using other byte sizes, that's why sometimes, especially in network protocols, it is called octet) With a byte of eight bits you can represent 256 values. (From 0 to 255) over multiple decades there were discussions on representing text (actually that was prr-computer already with telegraphs etc) and one found the ASCII rules to map between numeric values and characters. Each letter in the English alphabet got a code. Thus the word Hello becomes the sequence of numbers 72 101 108 108 111 Now decimal system ia nice when counting with fingers, but not so good for a binary machine. As said we have the byte, thus instead ten digits is inconvenient so we use a number system with base 16, thus any digit can not only have ten different values (0 to 9) but 16 (0 to 9 and A to F) Thus the same numbers as above can be written in hex 0x48 0x65 0x6C 0x6C 0x6F Now each byte is represented by two hex digits which gives a nice structure (and if you notice that 0x41 is A and 0x61 is a you start to understand ASCII) Now over time 256 characters wasn't enough for represting text in all languages, so after many years and messing around with different code pages and encodings smart folks came up with Unicode and Utf-8 to encode all characters. Won't go there too deep, but in essence utf-8 uses the ASCII table for the first 128 characters (half a byte, the lower half) and all other characters use multiple bytes. The German letter Ä fornisntance is 0xC3 0x48. In addition the Unicode standard has specific rules on sorting and comparing those values and some fancy transformations. (Sorting rules are even language dependent, so the German Ä can be sorted like AE, behind Z or simply as A) Wenn Browsers and JavaScript were created it was decided that a string in JavaScirpt shall be Unicode data. This is logical as websites are full of human readable text and properties of Unicode are good (well, the story is more complicated there, but good enough) This was good for the time where Web was working with simple websites and JavaScript was doing minor text manipulation. However for lower layers of the compitong stack data is just a sequence of bytes. In memory you have bytes. A file is a sequence of bytes. The meaning of those bytes might be Unicode text, might be an image, might even be bianry program code all are just bytes. The difference is only in the one looking at it. Now treating all data as unicode string will do harm, as they have all the assumptions on top where only specific byte sequences are valid and so on. So something else had to be added. And in fact different groups added different things. Before we can explain that, one step more is needed: as said interpretation depends on who looks at the data. So back to our numbers and from decimal and hex to binary. As said 8 bit are a byte. The number 1 in binary with it 8 bits would look like 0b00000001 Here using 0b as prefix, so you interpret it as binary, not hey, not decimal. The value 127 inn inary looks like this: 0b01111111 all bits, but the first set. Now something fancy happens when we set the first bit to 1 as well: 0b11111111 This can either be 255 or -1. Wait what negative!? You will ask: Yes, there eisna problem with these numbers: Our byte has to represent negative numbers as well. And then the first digit tells us whether it's negative or positive. And whenever we look at the byte we have to make a choice how we look at it. Is it a text character? Is it a positive number? A negative number? The byte doesn't know and doesn't care only we when looking at it. Now the Uint8Array enters! By giving a sequence of bytes (some area in memory) to an Uint8Buffer we tell it "look at those bytes and treat tit like a sequence of bytes, where each byte is a 8 bit unsigned number" and by that we can work with those.bytes and each byte will be a number 0 to 256 and whatever we do with the array decides on what it does with those bytes. Maybe it is a pixel in an image, maybe an frequency on an audio file, maybe ... Now another term there eisnthe Buffer. A buffer is just some area in memory. An arrayBuffer is "this is my area of memory, but I haven't yet decided how i will interpret it" I said Buffer, I said complicazednhsorory ... That tis that Node.js developers Hand this issue before ArrayBuffer d Uint8Array was defined, so they created their own thing, which is similar to an Unit8Array (modern versions of Buffer are built on Uint8Array) but don't be confused, buffer is a generic term for some data stored somewhere, which is distinct from Buffer, which is a type in Node.js. Now there ris a bit more. 256 is a bit low formmany purposes. So you can group multiple bytes and make them digits in a larger number. Quite typical are efor 4 byte, which are 32 bit. Thus a Int32Array takes 4 bytes at once from the underlying data (the ArrayBuffer, the buffer, the memory region) and makes it a 32 bit signed number (- 2147483648 to 2147483647) the Uint32Array (0 to 4294967295) and if that isn't enought there are big(u)int64Array, which look at 8 byte at a time, which is also the number of byte/bit a modern CPU can store in it's register, transfer though bus and process the best, hence 64bit architecture. The key thing is: memory (RAM, disk or any other form) in modern computers is just a sequence of bytes and the way you look at it decides on the meaning. The computer doesn't know and you have to tell it. Uint8Array is the typically representation for low level work. ArrayBuffer is when you haven't decided, yet and all else are more special.
2 of 2
1
it's a bit of a Pia esp when different libs want diff types. An ArrayBuffer is raw data. Like a big bag of data. You can't do anything with it - but if you convert it to something that can look inside the bag (Uint8Array), you can read / write it. If you just want to read, then Blob is your guy.
🌐
Quora
quora.com › What-is-the-relationship-between-a-Buffer-and-an-Uint8Array-in-Node-js
What is the relationship between a Buffer and an Uint8Array in Node.js? - Quora
Answer (1 of 3): tldr - Uint8Array is a general-purpose byte-array that’s available in both nodejs and browsers. Buffer is a subclass of Uint8Array that’s only available in nodejs (for historical reasons). both are primarily used for manipulating binary (byte) data. historically, when nodejs ...
🌐
JavaScript.info
javascript.info › tutorial › binary data, files
ArrayBuffer, binary arrays
July 11, 2022 - It’s the “eyeglasses” that give an interpretation of the bytes stored in the ArrayBuffer. ... Uint8Array – treats each byte in ArrayBuffer as a separate number, with possible values from 0 to 255 (a byte is 8-bit, so it can hold only ...
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
The Buffer class is a subclass of JavaScript's <Uint8Array> class and extends it with methods that cover additional use cases.
🌐
Medium
medium.com › weekly-webtips › javascript-lost-in-binaries-buffer-blob-uint8array-arraybuffer-ed8d2b4de44a
JavaScript: Lost in binaries — Buffer/Blob/UInt8Array/ArrayBuffer | by Naveen Kumarasinghe | Medium
April 2, 2023 - UInt8Array provides helpful functions to perform byte-level manipulations on ArrayBuffer. Unlike ArrayBuffer, UInt8Array can be sliced and resized.
🌐
GitHub
github.com › axios › axios › issues › 5589
body size difference using uint8array vs buffer for post request · Issue #5589 · axios/axios
March 9, 2023 - if I don't use Buffer.from and send data directly, the size seems 2x larger than data.byteLength. I have to use Buffer.from to make sure it has the same size.
Author: axios
Find elsewhere
🌐
Pavel Romanov
pavel-romanov.com › uint8array-vs-dataview-choosing-the-right-buffer-view-in-javascript
Uint8Array vs DataView: Best Buffer Choice - Pavel Romanov
August 20, 2024 - At the same time, there are differences in how they're editing buffer content. When dealing with typed arrays, we always work with a specific type of data. For example, using Uint8Array means that we only work with data range between 0 and 255.
Top answer
1 of 1
41

An ArrayBuffer is more than just a simple array. It contains raw binary data. This is very useful for direct memory manipulation and conserving space.

When you create a normal array, you won't get a proper set of contiguous memory in many cases since arrays can contain any combination of different kinds of objects. This is useful when you have dynamic data and multiple types together (frequently happens in JS) but is not so useful when you know the exact layout of memory that you need.

This also allows you to view the data at the byte level. For example, it's pretty common in binary data formats to have a n byte long identifier number, an m byte long field telling you how many bytes are used for this field, and m' bytes of data that actually makes up the data field.

[ identifier ][ bytes of data ][ data ]

With an ArrayBuffer, you have the option of moving through that data on the byte level by using various Views. A regular array doesn't allow you to move through the data with that level of granularity because no guarantees are made about the memory layout.

Finally, because you are telling the compiler/interpreter exactly how much space you're going to use and exactly how you're going to view it, it can do much more advanced optimizations when working with that data. When iterating through that data, it doesn't have to make calculated leaps through memory. Instead, it knows exactly how far to move ahead in memory to find the next data point.

As for what Uint8Array is, it's a typed array. Essentially, it tells the compiler/interpreter that you will be accessing this data exclusively as 8-bit uints which, again, allows it to make better optimizations. Then you can use standard array indexing on it (arr[0], arr[1], etc.) and you'll be getting back the equivalent uint values out of the array.

TL;DR They take less space when the exact data format is known, allows you to move more exactly through your data and gives the compiler/interpreter greater options for optimization.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array
Uint8Array - JavaScript - MDN Web Docs
Uint8Array is currently the only TypedArray subclass that has additional methods compared to other typed arrays. Because of its nature as a generic byte array, it is the most suitable for working with arbitrary binary data.
🌐
Bun
bun.sh › guides › binary › typedarray-to-buffer
Convert a Uint8Array to a Buffer - Bun
Use Buffer.from() to create a Buffer instance from a Uint8Array.
🌐
JavaScripture
javascripture.com › Uint8Array
Uint8Array JavaScript API
Changes to the items in this actual affect the underlying buffer, and vice versa.
🌐
Webdevtutor
webdevtutor.net › blog › javascript-buffer-vs-uint8array
JavaScript Buffer vs Uint8Array: A Comprehensive Comparison
While both JavaScript Buffer and Uint8Array serve similar purposes of handling binary data, there are some notable differences between the two. Buffer provides a higher-level API for working with binary data and is more versatile in terms of data manipulation and encoding.
🌐
Duktape
wiki.duktape.org › howtobuffers2x
How to work with buffers in Duktape 2.x
For very low memory environments plain buffers can be used in places where an Uint8Array would normally be used. Plain buffers mimic Uint8Array behavior quite closely for ECMAScript code so often only small ECMAScript code changes are needed when moving between actual Uint8Arrays and plain buffers.
🌐
GitHub
github.com › nodejs › node › issues › 41588
Start moving to Uint8Array in new APIs? · Issue #41588 · nodejs/node
January 19, 2022 - bufferIssues and PRs related to the buffer subsystem.Issues and PRs related to the buffer subsystem.discussIssues opened for discussions and feedbacks.Issues opened for discussions and feedbacks. ... There was a suggestion by @jasnell to use Uint8Arrays in new APIs over Buffers as well as a weigh-in by @sindresorhus saying it is easier to author cross-platform APIs when using Uint8Arrays.
Author: nodejs
🌐
W3Schools
w3schools.com › js › js_arraybuffers.asp
JavaScript Array Buffer
// Create an ArrayBuffer const myBuf = new ArrayBuffer(8); // Create a Uint8Array view const view = new Uint8Array(myBuf); // Write max 8 values view[0] = 10; view[2] = 128; view[1] = 255; // Read values let v0 = view[0]; let v1 = view[1]; let v2 = view[2]; Try it Yourself »
🌐
GitHub
github.com › tonistiigi › uint8
GitHub - tonistiigi/uint8: Convert between Uint8Array and Buffer without memory copy in Browserify. · GitHub
Convert Buffer to Uint8Array. Fast route only possible if original buffer(or its parent) was created from with uint8ToBuffer() function.
Author: tonistiigi