What do you use TypedArrays for in JavaScript/TypeScript?

8 points by aapoalas


I've recently been using TypedArrays more for manual memory management / custom data storage, and find myself somewhat struggling with the lack of good typing support in TypeScript w.r.t. TypedArrays. eg. Even if I can define a number value type union such as

type Kind = 0 | 1 | 2 | 3 | 4 | 5;

I have no way of defining a Uint8Array that contains only this Kind type. After working around this with arr[i] as Kind casting and getting bit a few times by refactorings breaking the code but the as cast types not catching this, I opened up a feature suggestion for TypeScript (the pits, I know...) to support this. One response I got was to say that this is a niche use-case, and that made me wonder:

What are JavaScript/TypeScript developers actually using TypedArrays for, if number is a sufficient value type in all but niche use-cases? If you have open-source code examples to point to, that's awesome, but I'm interested in user stories / written word as well.

aardvark179

Is your Kind really an integer with a narrow range, or is it an enum that you’re trying to optimise to a single byte because that’s the level you’ve been thinking at?

An array of integers within a restricted range is a pretty niche type. Enum arrays may be a somewhat less niche case, but enums are better represented as objects so you can’t do silly things, and if you are storing enough that size is that important you can just write an opaque class for doing the translation between number and enum and present a nicely typed interface.

Edit: oh and my biggest use of typed arrays has been for creating doubles from specific bit patterns, because JS doesn’t have nice ways to do this, and I had to do some testing of runtime methods for correctness.

snej

Other than as blobs, the most complex thing I’ve used them for is as database pages: a 4KB ArrayBuffer contains a b-tree node. This is a pretty complex data structure that's tricky to represent even in C++, with a variable-length array of 16-bit “pointers” to variable-size key-value pairs. Yeah, I have accessors that get and set numbers as branded-type wrappers.