title: Speeding up simple math operations on numeric 2D array (matrix) in JavaScript
body: <p>I have a numeric 2D array (an array of arrays, or a matrix) and I need to do simple matrix operations like adding a value to each row, or multiplying every value by a single number.  I have little experience with math operations in <code>JavaScript</code>, so this may be a bone-headed code snippet.  It is also very slow, and I need to use it when the number of columns is 10,000 - 30,000.  By very slow I mean roughly 500 ms to process a row of 2,000 values.  Bummer.</p>
  
  <pre><code>var ran2Darray = function(row, col){
      var res = [];
      for (var i = 0 ; i &lt; row; i++) {
          res[i] = [];
          for (var j = 0; j &lt; col; j++) {
              res[i][j] = Math.random();
          }
      }
      return res;
  }
  
  var myArray = ran2Darray(5, 100);
  var offset = 2;
  
  for (i = 0; i &lt; myArray.length; i++) {
      aRow = myArray[i];
      st = Date.now();
      aRow.map(function addNumber(offset) {myArray[i] + offset*i; })
      end = Date.now();
      document.write(end - st);
      document.write("&lt;/br&gt;");
      myArray[i] = aRow;
  }
  </code></pre>
  
  <p>I want to avoid any added libraries or frameworks, unless of course, that is my only option.  Can this code be made faster, or is there another direction I can go, like passing the calculation to another language?  I'm just not familiar with how people deal with this sort of problem. <code>forEach</code> performs roughly the same, by the way.</p>