Quats data in typed arrays instead of object properties

The Vec3 class is implemented using Float32Arrays, just curious why it’s not the same with the Quat class?

I can’t actually remember. Something to be aware of is that typed array creation is very slow. But they’re fast to read/write to. If not sure it’d be much of a win to switch to typed arrays but I’m open-minded on it. :slight_smile:

Take a look at the performance tests below. I have run them on chrome on my Windows 10 machine and the results are interesting. When comparing the current object implementation of Quats with a Float32Array and Array implementation, it looks like setting and getting performance is essentially the same across the board. Float32Arrays don’t really perform better, and they are quite slower to create.

My guess is that the reason Float32Arrays are used in the Vec and Matrix classes is that WebGL expects vectors and matrices to be passed in Float32Arrays. Since I don’t think Quats are being passed directly to WebGL, it was probably decided that it wouldn’t be advantageous to implement them using Float32Arrays.

http://jsben.ch/d69YN
https://jsperf.com/quaternion-float32array-vs-array-vs-object

Interesting benchmarks. I did similar tests a few years back and things were a bit different back then.

You might be right about why vectors and matrices are typed arrays. We do indeed pass them straight to WebGL. However, we could organize things slightly differently. Whenever we create a shader uniform, we could allocate a typed array. And then, when the uniform is set, we just copy the data from the source object to the uniform’s typed array. There will be far fewer uniforms in a running app than vectors + matrices + colors.

1 Like

Reading from typed arrays is indeed faster than from normal arrays as far as my tests tell me, BUT you probably need more elements to see any difference.

1 Like