Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Float32Array instead of Array in JavaScript

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

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.

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]; 

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.

like image 335
pancake Avatar asked Apr 04 '13 22:04

pancake


People also ask

What is Float32Array?

The Float32Array typed array represents an array of 32-bit floating point numbers (corresponding to the C float data type) in the platform byte order. If control over byte order is needed, use DataView instead. The contents are initialized to 0 .

When would you use an array in JavaScript?

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.

Are JavaScript arrays actually arrays?

The humble creators of JavaScript have provided us with the typeof operator. typeof is used to know the type of the variable in question. typeof used on an array returns Object rather than Array . We get this result because arrays are not really arrays in JavaScript.

What is a TypedArray?

A TypedArray object describes an array-like view of an underlying binary data buffer. There is no global property named TypedArray , nor is there a directly visible TypedArray constructor.


1 Answers

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

  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.

  2. 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.

  3. 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:

  • 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).

like image 118
pancake Avatar answered Oct 11 '22 12:10

pancake