当在JavaScript中使用,而不是阵列Float32Array [英] When to use Float32Array instead of Array in JavaScript

查看:1652
本文介绍了当在JavaScript中使用,而不是阵列Float32Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当它是有意义的使用,而不是 Float32Array 标准的JavaScript 阵列的浏览器应用程序?

When does it make sense to use a Float32Array instead of a standard JavaScript Array for browser applications?

性能测试显示 Float32Array 是,一般来说,速度较慢的 - 如果我理解正确的标准阵列门店数作为64位 - 所以在precision没有优势。

This performance test shows Float32Array to be, in general, slower - and if I understand correctly a standard Array stores numbers as 64bit - so there is no advantage in precision.

除了从任何可能的性能损失, Float32Array 也有可读性的缺点 - 不必使用构造函数:

Aside from any possible performance hit, Float32Array also has the disadvantage of readability - having to use a constructor:

a = new Float32Array(2);
a[0] = 3.5;
a[1] = 4.5;

而不是一个数组文本

instead an array literal

a = [3.5, 4.5];

我问这是因为我使用的库glMatrix默认为 Float32Array - 并且知道是否有任何理由,我不应该强制使用阵列来代替,这将允许我使用数组常量。

I'm asking this because I'm using the library glMatrix which defaults to Float32Array - and wondering if there's any reason I shouldn't force it to use Array instead which will allow me to use array literals.

推荐答案

我给开发商 glMatrix 和我的回答如下,包括他的意见(点2和3)

I emailed the developer of glMatrix and my answer below includes his comments (points 2 & 3):


  1. 创建一个新的对象一般具有更快阵列 Float32Array 是。增益是小数组显著,但较少(与环境相关)与更大的阵列。

  1. Creating a new object is generally quicker with Array than Float32Array. The gain is significant for small arrays, but is less (environment dependent) with larger arrays.

访问的从TypedArray(如 Float32Array )的数据往往比从一个正常的阵列,这意味着大多数阵列快操作(除了创建一个新的对象)与TypedArrays更快。

Accessing data from a TypedArray (eg. Float32Array) is often faster than from a normal array, which means that most array operations (aside from creating a new object) are faster with TypedArrays.

正如@emidander还指出, glMatrix 主要是为WebGL的,这需要向量和矩阵作为传递开发 Float32Array 。因此,对于WebGL的应用,从阵列 Float32Array 潜在的昂贵的转换需要被包含在任何性能测试

As also stated by @emidander, glMatrix was developed primarily for WebGL, which requires that vectors and matrices be passed as Float32Array. So, for a WebGL application, the potentially costly conversion from Array to Float32Array would need to be included in any performance measurement.

所以,毫不奇怪,最好的选择是依赖于应用程序:

So, not surprisingly, the best choice is application dependent:


  • 如果阵列一般都很小,​​和/或对它们的操作数是低,使得构造时间是数组的寿命的一个显著比重,使用阵列

如果code可读性和性能同样重要,然后用阵列(即使用 [] ,而不是一个构造函数)。

If code readability is as important as performance, then use Array (i.e. use [], instead of a constructor).

如果阵列是非常大的和/或用于许多操作,然后用TypedArray。

If arrays are very large and/or are used for many operations, then use a TypedArray.

有关WebGL的应用程序(或其他应用程序,否则将需要一个类型转换),可以使用 Float32Array (或其他TypedArray)。

For WebGL applications (or other applications that would otherwise require a type conversion), use Float32Array (or other TypedArray).

这篇关于当在JavaScript中使用,而不是阵列Float32Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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