I need a typed array, specifically a Float32Array, of all zeros. I was thinking that I would have to clear it manually, but I noticed that when I declared it, it was already zeroed out. Is this something that is specified in the spec? Can I rely on this behavior?
Share
According to JavaScript’s Typed Array Specification, contents are initialized to 0. So you should be able to rely on this behavior.
Be aware that typed arrays don’t have very good cross browser support yet. Chrome, Safari, Firefox, and Opera support it, but Internet Explorer only introduced support in IE10.
I should also mention that typed arrays are currently extremely slow in Safari compared to normal arrays. For this reason, you’re probably better off avoiding typed arrays unless you aren’t targeting Safari. Using normal arrays, all array values are initialized as
undefined.