What do you use TypedArrays for in JavaScript/TypeScript?
8 points by aapoalas
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.
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.
I need to sleep so a proper response will have to wait, but I've actually written about this specific case a few weeks ago: https://trynova.dev/blog/interlude-a-data-oriented-model
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.
Thank you; that sounds pretty cool indeed! Out of interest, are you branding both the indexes for access (pointers) and values to store, or only one or the other? It sounds like you'd probably have multiple different branded types, representing offsets to different parts of the b-tree node.
I don’t remember exactly. I know I had a ‘Ptr’ type representing 16-bit offsets into a page, and a ‘PageNo’ type representing a 32-bit page number (in the interior pages, the value of a key is a PageNo not a string.)
It’s an unfinished project. I got the basic persistent B-tree stuff working; the next task was to support keys or values too large to fit in a page; there are many ways to do that, and I hadn’t settled on one.
Something worth noting, typed Javascript may be a syntactic superset of Javascript, but the experience of writing typed Javascript is more like the experience of working in totally different programming language. There's lots of things that are strong idioms when working in untyped JS that become antipatterns in TS because they don't tickle the compiler quite right.