Unit8Array is a typed array represents an array of 8-bit unsigned
integers.
This is what I learnt from MDN.
But why should we use Unit8Array specifically?
Why not use a plain array instead?
Why is Unit8Array often related to binary data?
这是我从MDN学到的。但是为什么我们要特别使用Unit8Array呢?为什么不使用普通数组呢?为什么Unit8数组经常与二进制数据相关?
更多回答
read MDN docs on ArrayBuffer, DataView - see if that enlightens you
阅读有关ArrayBuffer、DataView的MDN文档-看看这是否会对您有所启发
@MichalBurgunder I have read that answer and no, it does not clear my confusion.
@MichalBurgUnder我已经读过这个答案,不,它并没有澄清我的困惑。
Unit8Array uses less memory. So for memory heavy applications (e.g. image recognition), this will save signficant amount of space. Plus, it's typed, so you can be sure that binary data in this array isn't "corrupted" with strings, objects, etc. It's also cleaner to program with, because other developers reading your code know what's in it. Does that help? If not, what are you confused about?
单元8数组使用较少的内存。因此,对于内存密集型应用程序(例如图像识别),这将节省大量空间。此外,它是类型化的,所以您可以确保此数组中的二进制数据不会被字符串、对象等“破坏”。它的编程也更干净,因为阅读您的代码的其他开发人员知道其中有什么。这有帮助吗?如果不是,你有什么困惑的?
In essence, if the Javascript memory allows for it, yes. Buffers are basically glorified arrays, used for moving around unformatted data (e.g. pure binary data). Putting them into an array, you might get the type from (number vs String), which could cause huge problems. Which buffer/typed array you use, depends on the data you're dealing with. Black white photos, maybe an Int8Array. Color, perhaps Int16Array. It all depends on what you're doing.
从本质上说,如果Javascript内存允许的话,是的。缓冲区基本上是美化的数组,用于移动未格式化的数据(例如纯二进制数据)。将它们放入数组中,您可能会从(数字与字符串)中获得类型,这可能会导致巨大的问题。您使用的缓冲区/类型化数组取决于您正在处理的数据。黑白照片,可能是Int8阵列。颜色,可能是Int16阵列。这完全取决于你在做什么。
优秀答案推荐
Type insurance
Let's suppose you want numeric, or specifically Uint8 values at some function for the purpose of analyzing or processing bytes. If a Uint8Array
is passed to this function, then the function does not have to make safety checks and validations on the type and the code will be cleaner.
让我们假设为了分析或处理字节,您需要某个函数中的数值,特别是Uint8值。如果将Uint8数组传递给此函数,则该函数不必对类型进行安全检查和验证,代码将更加干净。
Nonverbal communication
Other developers will have an easy time understanding what values such an array will contain, while an array with unspecified element type may contain "anything".
其他开发人员将很容易理解这样的数组将包含什么值,而具有未指定元素类型的数组可能包含“任何”。
Memory optimization
Let's suppose you are storing 1024 integer values in this array. If you use up a single byte per item, then you are using only 1 KB for the items of this array.
假设您在此数组中存储1024个整数值。如果每个项都用完了一个字节,那么这个数组中的项就只使用了1 KB。
更多回答
我是一名优秀的程序员,十分优秀!