Uint8Array Javascript用例 [英] Uint8Array Javascript usecase

查看:699
本文介绍了Uint8Array Javascript用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现Javascript通过此类型键入了数组链接.我立即好奇这样的对象在语言中可能带来什么好处.

I just discovered that Javascript has typed arrays via this link. I was immediately curious what the benefit of such objects might be in the language.

我注意到UInt8Arrays失去了普通数组对象具有的.map()类型函数,因此,如果要循环遍历它们,则需要一个for循环.

I noticed that UInt8Arrays lose the .map()-type functions that I would have for normal arrays objects so if you want to loop over them you would need a for loop.

我假设使用UInt8Arrays时可以期望获得一些性能提升,但事实并非如此.

I assumed that I might be able to expect some performance boost when using UInt8Arrays but this doesn't seem to be the case.

var a = d3.range(225)
var b = new Uint8Array(d3.range(225))

console.time("a")
var result = 0;
for (var j = 10000; j >= 0; j--) {
    for (var i = a.length - 1; i >= 0; i--) {
        result += a[i];
    };
};
console.timeEnd("a")
console.time("b")
var result = 0;
for (var j = 10000; j >= 0; j--) {
    for (var i = b.length - 1; i >= 0; i--) {
        result += b[i];
    };
};
console.timeEnd("b")

我正在使用d3库快速生成大型数组.该脚本提供以下输出:

I am using the d3 library to quickly generate a large array. This script gives the following output:

a: 2760.176ms
b: 2779.477ms 

因此性能不会提高.插入错误的值时,UInt8Array也不会引发错误.

So the performance doesn't improve. The UInt8Array also doesn't throw an error when you insert a wrong value.

> new Uint8Array([1,2,3,4,'aasdf'])
[1,2,3,4,0]

考虑到这一点,JavaScript中UInt8Array的正确用例是什么?看来普通阵列要灵活得多,同样健壮且同样快.

With this in mind, what is the proper use case for UInt8Array in Javascript? It seems like the normal array is a lot more flexible, equally robust and equally fast.

推荐答案

性能"通常并不意味着脚本可以运行多快.还有许多其他重要因素,例如冻结计算机的速度和/或崩溃的速度.记忆.通常为var分配的javascript实现的最小内存量是32位.这意味着

"Performance" usually doesn't mean just how fast your script runs. There are also many other important factors, like how fast it freezes your pc and/or crashes. memory. The smallest amount of memory javascript implementation usually allocate for a var is 32 bit. This means

var a = true;

在您的记忆中一个布尔值看起来像这样:

a boolean looks like this in your memory:

0000 0000 0000 0000 0000 0000 0000 0001

这是一个巨大的浪费,但通常不是问题,因为没有人使用足够多的东西来真正解决问题.类型数组适用于确实重要的情况,当您实际上可以大量减少内存使用时,例如处理图像数据,声音数据或各种原始二进制数据时.

It's a huge waste, but usually not a problem, as no one uses a significant enough amount of them for it to really matter. Typed Arrays are for cases where it does matter, when you can actually reduce your memory usage by a huge amount, like when working with image data, sound data, or all sorts of raw binary data.

另一个区别是,在某些情况下,它可以使您潜在地节省更多的内存,它允许您对通常按值传递的按引用传递的数据进行操作.

Another difference, that allows you to potentially save even more memory in some cases is that it allows you to operate on data passed by reference you'd normally pass by value.

考虑这种情况:

var oneImage = new Uint8Array( 16 * 16 * 4 );
var onePixel = new Uint8Array( oneImage.buffer, 0, 4 );

您现在在同一个ArrayBuffer上有2个独立的视图,对相同的数据进行操作,应用这一概念不仅可以使您的内存中拥有一个巨大的东西,还可以将其实际细分为与您一样多的段当前希望以很少的开销进行工作,这可能更加重要.

you now have 2 independent views on the same ArrayBuffer, operating on the same data, applying that concept allows you to not only have that one huge thing in your memory, it allows you to actually subdivide it into as many segments as you currently want to work on with little overhead, which is probably even more important.

这篇关于Uint8Array Javascript用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆