Skip to content Skip to sidebar Skip to footer

Uint8array Javascript Usecase

I just discovered that Javascript has typed arrays via this link. I was immediately curious what the benefit of such objects might be in the language. I noticed that UInt8Arrays l

Solution 1:

"Performance" usually doesn't mean just how fast your script runs. There are also many other important factors, like how fast it freezes your pc and/or crashes. memory. The smallest amount of memory javascript implementation usually allocate for a var is 32 bit. This means

var a = true;

a boolean looks like this in your memory:

0000 0000 0000 0000 0000 0000 0000 0001

It's a huge waste, but usually not a problem, as no one uses a significant enough amount of them for it to really matter. Typed Arrays are for cases where it does matter, when you can actually reduce your memory usage by a huge amount, like when working with image data, sound data, or all sorts of raw binary data.

Another difference, that allows you to potentially save even more memory in some cases is that it allows you to operate on data passed by reference you'd normally pass by value.

Consider this case:

var oneImage = newUint8Array( 16 * 16 * 4 );
var onePixel = newUint8Array( oneImage.buffer, 0, 4 );

you now have 2 independent views on the same ArrayBuffer, operating on the same data, applying that concept allows you to not only have that one huge thing in your memory, it allows you to actually subdivide it into as many segments as you currently want to work on with little overhead, which is probably even more important.

Post a Comment for "Uint8array Javascript Usecase"