29

In the documentation 1 of the fs module, we can read (for the writeFile method):

const data = new Uint8Array(Buffer.from('Hello Node.js'));

In the same documentation 2 it is said:

With TypedArray now available, the Buffer class implements the Uint8Array API in a manner that is more optimized and suitable for Node.js.

So if the Buffer class implements a Unint8Array, could you enlighten me why we need to convert to an Unint8Array from a Buffer?

1
  • 1
    @PatrickRoberts done :) Commented Feb 21, 2019 at 22:42

1 Answer 1

58

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

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.