title: Json to csv in node.js
body: <p>I am trying to convert a very large json to csv in node.js but it is taking too much time and also leading to 100% cpu at the time of conversion.</p>
  
  <pre><code>  jsonToCsv: function (data) {
      var keys = Object.keys(data[0]);
      var csv = [keys.join(",")];
      console.time("CSVGeneration");
      data.forEach(function (row) {
        var line = ''; 
        keys.forEach(function (key) {
          if (typeof row[key] === 'string') {
            row[key] = "" + file_utils.escapeCsv(row[key]) + "";
          } 
          line += row[key] + ",";
        });
        csv.push(line);  
      });
      console.timeEnd("CSVGeneration");
      csv = csv.join("\n");
      return csv;
    },
    escapeCsv: function (x) {
      if (x)
        return ('' + x.replace(/"/g, '').replace(/,/g, ' ').replace(/\n/g, " ").replace(/\r/g, " ") + '');
      else
        return ('');
    },
  </code></pre>
  
  <p>On an average run for 1Lac rows, it never recovered to even log time. I had to kill the process manually.</p>
  
  <p>Can someone suggest a better alternative to this?</p>