Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to populate a javascript typed array?

What is the best way to populate a javascript typed array with literal data?

Recently I've been working a lot with javascript typed arrays for some mathematical work. In particular, I'm using lots of Float32Array objects.

I often have to manually populate their values. With a regular array, there is the following literal syntax available: var a = [0,1,2]; But there seems to be no equivalent way to populate a typed array, so I find I have to do it with lots of individual statements;

var a = new Float32Array(3);
a[0] = 0;
a[1] = 1;
a[2] = 2;

This gets very annoying if there's more than about 4 values. And it seems wasteful too, both in terms of script size, and javascript execution, because it has to interpret all those individual statements.

Another approach I've used is creating a setter function:

var populateArray(a,vals) {
    var N = vals.length;
    for (var i=0; i<N; i++) {a[i]=vals[i];}
};
var a = new Float32Array(3);
populateArray(a,[0,1,2]);

But this seems quite wasteful too. I have to create a regular array as big as the typed array first, to feed into the populateArray function, and then there's the iteration.

So... the question is: Is there a more direct and efficient way to fill a typed array with literal data?

like image 822
Daniel Howard Avatar asked Feb 25 '14 14:02

Daniel Howard


People also ask

How do you populate an array in JavaScript?

In JavaScript, you can use the Array. fill() method to populate an array with a zero or any other value like an object or a string. This method replaces all elements in an array with the value you want to populate the array with and returns the modified array.

How do you populate an array?

After you create an array, we can start storing values in to the array. To populate an array with values, you need to use the name of the array, the index (indicated inside square brackets []) where you want to store a value, and the value you want to store. For instance, var URLsArray = new Array (4);

Why might you use typed arrays instead of standard arrays?

Normal arrays can be as fast as typed arrays if you do a lot of sequential access. Random access outside the bounds of the array causes the array to grow. Typed arrays are fast for access, but slow to be allocated. If you create temporary arrays frequently, avoid typed arrays.

How would you fill static values in a given array in JS?

Using push() method: We use the push() method to fill static values in an array in JavaScript. We use the push() method inside the for() loop. The push() method always adds a new element to the end of the array so that means the push() method cannot replace the elements of the array.


1 Answers

Why not simply do new Float32Array([1,2,3]);

Look at the constructor overloads:

Float32Array Float32Array( unsigned long length );

Float32Array Float32Array( TypedArray array );

Float32Array Float32Array( sequence array );

Float32Array Float32Array( ArrayBuffer buffer, optional unsigned long byteOffset, optional unsigned long length );

like image 122
Johan Avatar answered Sep 19 '22 09:09

Johan