Is there a way to set a typed array to zero?

Is there a way to set each Javascript element of a typed array (i.e. a Uint32Array ) to some value (something like C, the memset function will do)?

 var foo = new Uint32Array(16384); for (int i=0; i<foo.length; i++) { // I want to do this without a for-loop foo[i] = 0xdeadbeef; } 
+4
source share
1 answer

As a rule, the answer in this case will be negative. In JavaScript, you either fully declare when you create them using alphabetic syntax:

 var Arr1 = [1,2,3,4,5]; 

Or you assign values ​​to them (using loops when necessary for sequences):

 var Arr2 = Array(32); for (var i = 0, j < Arr2.length; i < j; ++i) { Arr2[i] = 0xdeadbeef; } 

JavaScript is a language that whenever possible uses only access to Arr2.length, so this syntax should provide a performance advantage over other options, but there is no way to assign all positions in the array to a specific value other than undefined, which you get when initializing with size.

-2
source

All Articles