javascript - Node.js - Buffer 与 Uint8Array

标签 javascript node.js buffer uint8array

在文档中1 fs 模块的内容,我们可以读取(对于 writeFile 方法):

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

在同一文档中2据说:

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

如果 Buffer 类实现了 Unint8Array,您能告诉我为什么我们需要从 Buffer 转换为 Unint8Array 吗?

最佳答案

Uint8Array 是一个通用字节数组,在 Nodejs 和浏览器中都可用。 Buffer 是 Uint8Array 的子类,仅在 Nodejs 中可用(由于历史原因)。两者主要用于操作二进制(字节)数据。

从历史上看,当nodejs第一次出现时,通用的Uint8Arrays还不存在,所以它必须发明自己的“Buffer”类型来处理二进制数据。在es6引入通用Uint8Arrays之后,nodejs(4.0版本之后)决定从单独的数据类型-> Uint8Array的子​​类迁移Buffer(以尝试使其与Uint8Array在浏览器上更加兼容)。

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

关于javascript - Node.js - Buffer 与 Uint8Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54814215/

相关文章:

javascript - 如何配置 puppeteer 以获得包含完整图像和样式的 pdf 页面?

mysql - 通过 NodeJS 访问远程 MySQL (3306) 返回 No Route (113 MySQL)

javascript - onbeforeunload javascript 调用 backing bean 方法

javascript - 从 Apps 脚本在 Google Apps 中创建用户

node.js - nodejs-serialport => 关闭后重新建立与端口的连接

buffer - hlsl 中使用的常量缓冲区 (cbuffer) 究竟是什么?

c++ - OpenGL - 渲染到纹理中

c++ - libevent bufferevent 的 evbuffer_add

javascript - 如何在javascript函数中添加html文件?

javascript - 在 Anime.js 中使用 SVG 对象 HTML