gpt4 book ai didi

javascript - Node.js - Buffer 与 Uint8Array

转载 作者:行者123 更新时间:2023-12-03 00:09:25 26 4
gpt4 key购买 nike

在文档中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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com