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