I found this question for C, which discusses the 2d vs 1d array performance.
I would like to know the same for javascript
/ typescript
, as I'm trying to build a lib which involves many matrix operation.
Currently, I'm using 2d array:
export
type Matrix = Array<Array<number>>;
With this approach, we can access matrix element by m[row][col]
Also, matrix can be implemented using 1d array, something like this:
Class Matrix {
row: number;
col: number;
data: Array<number>;
}
with this approach, we can access matrix element(i, j) by m.data[i*col+row]
which would be better at performance? or maybe there're other solutions for matrix.
Thanks!