简体   繁体   English

Javascript将Uint8Array [x]分配给新的var:类型?

[英]Javascript assigning Uint8Array[x] to new var: type?

If I have the following code: 如果我有以下代码:

var uInt8Array = new Uint8Array([0, 1, 2]);
var number = uInt8Array[1];

Will number be parsed to a 64-bit float or will it stay a 8-bit unsigned integer? 将数字解析为64位浮点数还是保留为8位无符号整数?

The Uint8Array datatype exists for the purpose of saving memory in some specific case. 存在Uint8Array数据类型是为了在某些特定情况下节省内存。 The savings in memory are fairly unimportant when dealing with single elements, but become important when dealing with large arrays, since each element will consume 1/8th the memory that it would normally. 内存的节省在处理单个元素时并不重要,但在处理大型数组时变得很重要,因为每个元素将消耗正常内存的1/8。

Examples where this becomes important are when dealing with media, ie audio,images and video. 当处理媒体(即音频,图像和视频)时,这变得很重要的示例。

A 1 mega-pixel image will be stored as 4 million elements, each of 8bits. 一个1百万像素的图像将存储为4百万个元素,每个8位。 8bits for each of r, g, b and a. r,g,b和a分别为8位。 This will of course, consume 4,000,000 bytes. 当然,这将消耗4,000,000字节。 If this datatype did not exist and images were still stored in arrays with 4 elements, this memory requirement would balloon-out to 32,000,000 bytes. 如果此数据类型不存在,并且图像仍存储在具有4个元素的数组中,则此内存要求将膨胀到32,000,000字节。

You can test this yourself by writing a simple test-code and placing watches on the variables. 您可以通过编写简单的测试代码并将监视放在变量上来自己进行测试。 The initial array is shown to be a Uint8Array, while the element extracted from it is shown to be a standard number object. 初始数组显示为Uint8Array,而从数组中提取的元素显示为标准数字对象。

Here's a screen-shot of the code you present and the variables in a watch window. 这是您显示的代码和监视窗口中的变量的屏幕截图。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM