Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  42] [ 5]  / answers: 1 / hits: 18522  / 10 Years ago, wed, june 25, 2014, 12:00:00

I can't use .push() on a Float32Array, I get an error, so I tried to add it like this :



myarray = new Float32Array() ;
myarray.push = function()
{
for( var i in arguments )
{
this[this.length] = arguments[i] ;
}
} ;


But it does not work. I do not get errors, but my array's values are all 0.
Why ?


More From » arrays

 Answers
8

Basically a Float32Array is just a view over an ArrayBuffer object (as are all typed arrays in JS). This ArrayBuffer has a fixed length, which in turn the Float32Array inherits.



So to conclude: You just can not change the size of your Float32Array on the fly. The only possibility would be as this:




  1. Create a new array with the length of the old array + 1

  2. Copy all items of the old array over to the new array.

  3. Insert the new item as the last item in the new array.

  4. Replace all instances of the old array with the new array.



If you want to add multiple values on multiple occasions, I strongly advise against this. This completely negates any performance advantage you might gain from using typed arrays!


[#70438] Monday, June 23, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emiliotristinv

Total Points: 181
Total Questions: 99
Total Answers: 96

Location: Argentina
Member since Fri, Oct 21, 2022
2 Years ago
emiliotristinv questions
Wed, Jun 16, 21, 00:00, 3 Years ago
Tue, May 12, 20, 00:00, 4 Years ago
Tue, Mar 31, 20, 00:00, 4 Years ago
;