何时在 JavaScript 中使用 Float32Array 而不是 Array
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15823021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use Float32Array instead of Array in JavaScript
提问by pancake
When does it make sense to use a Float32Array
instead of a standard JavaScript Array
for browser applications?
什么时候对浏览器应用程序使用Float32Array
标准 JavaScript 而不是标准 JavaScript有意义Array
?
This performance testshows 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
,一般来说,速度较慢——如果我理解正确的话,标准Array
将数字存储为 64 位——所以在精度上没有优势。
Aside from any possible performance hit, Float32Array
also has the disadvantage of readability - having to use a constructor:
除了任何可能的性能影响之外,Float32Array
还有可读性的缺点 - 必须使用构造函数:
a = new Float32Array(2);
a[0] = 3.5;
a[1] = 4.5;
instead an array literal
而是一个数组文字
a = [3.5, 4.5];
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.
我问这个是因为我正在使用默认为的库 glMatrixFloat32Array
并且想知道是否有任何理由我不应该强制使用它来Array
代替它,这将允许我使用数组文字。
回答by pancake
I emailed the developer of glMatrix
and my answer below includes his comments (points 2 & 3):
我给开发人员发了电子邮件,glMatrix
下面的回答包括他的评论(第 2 点和第 3 点):
Creating a new object is generally quicker with
Array
thanFloat32Array
. The gain is significant for small arrays, but is less (environment dependent) with larger arrays.Accessingdata 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.As also stated by @emidander,
glMatrix
was developed primarily for WebGL, which requires that vectors and matrices be passed asFloat32Array
. So, for a WebGL application, the potentially costly conversion fromArray
toFloat32Array
would need to be included in any performance measurement.
创建新对象通常
Array
比使用更快Float32Array
。增益对于小阵列很重要,但对于较大的阵列则较小(取决于环境)。从 TypedArray(例如
Float32Array
)访问数据通常比从普通数组更快,这意味着大多数数组操作(除了创建新对象)使用 TypedArray 更快。正如@emidander 所述,
glMatrix
它主要是为 WebGL 开发的,它要求向量和矩阵作为Float32Array
. 因此,对于 WebGL 应用程序,从Array
到的潜在成本转换Float32Array
需要包含在任何性能测量中。
So, not surprisingly, the best choice is application dependent:
因此,毫不奇怪,最佳选择取决于应用程序:
If arrays are generally small, and/or number of operations on them is low so that the constructor time is a significant proportion of the array's lifespan, use
Array
.If code readability is as important as performance, then use
Array
(i.e. use[]
, instead of a constructor).If arrays are very large and/or are used for many operations, then use a TypedArray.
For WebGL applications (or other applications that would otherwise require a type conversion), use
Float32Array
(or other TypedArray).
如果数组通常很小,和/或对它们的操作数量很少,因此构造函数时间占数组生命周期的很大一部分,请使用
Array
.如果代码可读性和性能一样重要,那么使用
Array
(即使用[]
,而不是构造函数)。如果数组非常大和/或用于许多操作,则使用 TypedArray。
对于 WebGL 应用程序(或其他需要类型转换的应用程序),使用
Float32Array
(或其他 TypedArray)。
回答by emidander
I would assume that the glMatrix library uses Float32Array because it is primarily used in WebGL-applications, where matrices are represented as Float32Arrays (http://www.khronos.org/registry/webgl/specs/1.0/#5.14.10).
我假设 glMatrix 库使用 Float32Array,因为它主要用于 WebGL 应用程序,其中矩阵表示为 Float32Arrays ( http://www.khronos.org/registry/webgl/specs/1.0/#5.14.10)。