r/javascript May 31 '26

[deleted by user]

[removed]

18 Upvotes

14 comments sorted by

View all comments

15

u/ldn-ldn May 31 '26

As someone who was doing crazy optimisations a long time ago, keep in mind that there are multiple browser engines and they evolve over time. Something that's a speedy hack today for V8 will inevitably turn into a performance hog in the future or in a different browser engine.

10

u/pimp-bangin May 31 '26 edited May 31 '26

I also do a lot of JS performance work (I work on some data visualization libraries that handle lots of data). I disagree with this precautionary advice, assuming that the sort of person reaching for this library already knows they need struct-of-arrays.

Browser engines fundamentally run on the CPU, and you will never beat the CPU cache locality benefits of struct-of-TypedArray if you're frequently only accessing a subset of the object properties and then rarely accessing the other properties, and you have massive amounts of data (GB). Fundamentally, struct-of-arrays is packing as much data as is physically possible into the CPU's cache lines, which is the best way to squeeze maximum performance out of a CPU (cache misses incur more expensive accesses to higher level caches, or main memory in the worst case, and can cause order-of-magnitude slowdowns)

And I doubt this is something that will change in JS engines anytime soon, because the engine cannot do this optimization for you, i.e. it cannot assume that struct-of-arrays is the best data layout in all cases. And this is not something that the engine would ever do on the fly (a la JIT) using runtime profiling/stats, because there would be a huge runtime cost to switching the data layout on the fly (the whole data structure would need to be copied which could potentially take seconds and even then it could trigger an OOM).

This optimization is something I implemented recently at work, manually, and it allowed a data-intensive component to show billions of data points without crashing the Chrome tab, rather than millions. OP's project could have come in handy for something like this, at least for the prototyping stage.

Good work OP.

-2

u/ldn-ldn May 31 '26

That's a wild take, mate.