Is there a method to set all indices in an ArrayBuffer to 0 that is optimized to be fast? I know I can do an iteration to do it manually but I was wondering if there is some built-in that does it fast as I want to do this once per animation frame.
1 Answer
There is no reason to initialize the memory in an ArrayBuffer to 0, because this is automatically done when the array is created:
From MDN:
Return value
A new
ArrayBufferobject of the specified size. Its contents are initialized to 0.
This default dates back all the way to the obsolete initial spec, and all browsers should behave this way.
Besides, it stands to reason the memory would be cleared on allocation, else who knows what memory an attacker might be able to access.
For all other purposes:
If you need to initialize it to a value other than 0 or clear out existing data, you can use the native fill method which you can use on a typed array view (like Uint8Array) of the ArrayBuffer. Browser supports is not so great though, so you may want to load up a polyfill for old browsers.