I've found many ways to create a Float32Array from an array, but all of them involve copying all elements. I want to avoid that cost, because the array will never be used again. I just want to cast it. How is that possible?
1 Answer
It's impossible. A Float32Array is always a different object than a normal array.
If you want to avoid the cost of copying the items, a) don't use a normal array in the first place or b) let your function that uses it work with normal arrays as well.
If you are looking for the "casting" operation, you can just invoke the Float32Array constructor with the normal array:
var arr = [0.3, 1, 2];
var typedArr = new Float32Array(arr);
.slice()to do a copy.