Since JavaScript has variable-size arrays, multidimensional arrays in V8 and, as I believe, other JS engines are basically arrays of pointers to arrays. It’s a different story in C/C++ which have fixed-size arrays and represent multidimensional arrays with a contiguous chunk of memory.

If you use multidimensional arrays in JS for, say, calculations over matrices, a sane approach might be to store all elements in a single-dimension array and make sure that your code accesses elements in a CPU cache-friendly way. With this approach, you may get significantly better performance.

Andrey Pechkurov
Andrey Pechkurov

Written by Andrey Pechkurov

Core database engineer at QuestDB. Distributed systems gazer. Node.js contributor. Occasional tech blogger and speaker.

No responses yet