title: Javascript - .map running out of memory
body: <p>My libraries:</p>
  
  <pre><code>const Promise = require('bluebird');
  const fs = Promise.promisifyAll(require('graceful-fs'));
  const path = require('path');
  const xml2js = Promise.promisifyAll(require('xml2js'));
  </code></pre>
  
  <p>I have a large number of XML files I want to parse. I am able to create an array of paths to all the files using this function:</p>
  
  <pre><code>function getFileNames(rootPath) {
    // Read content of path
    return fs.readdirAsync(rootPath)
      // Return all directories
      .then(function(content) {
        return content.filter(function(file) {
          return fs.statSync(path.join(rootPath, file)).isDirectory();
        });
      })
      // For every directory
      .map(function(directory) {
        // Save current path
        let currentPath = path.join(rootPath, directory);
        // Read files in the directory
        return fs.readdirAsync(currentPath)
          // Filter out the XMLs
          .filter(function(file) {
            return path.extname(file) === '.XML';
          })
          // Return path to file
          .map(function(file) {
            return path.join(rootPath, directory, file);
          });
      })
      // Flatten array of results
      .reduce(function(a, b) {
        return a.concat(b);
      });
  }
  </code></pre>
  
  <p>and now I want to go trough every single file and parse it.</p>
  
  <p>I have 2 function to do so:</p>
  
  <pre><code>function openFile(filePath) {
   return fs.readFileAsync('./' + filePath)
    .then(function(fileData) {
      return fileData;
    });
  }
  
  function parseFile(data) {
    return xml2js.parseStringAsync(data)
        .then(function(xmlObject) {
          return xmlObject;
        });
  }
  </code></pre>
  
  <p>Now when I call this with the .map (the <code>GetFileNames</code> function outputs an array with over 20k strings with file paths) function:</p>
  
  <pre><code>getFileNames('./XML')
    .map(function(file) {
      openFile(file)
        .then(function(data) {
          parseFile(data)
              .then(function(object) {
                console.log(object);
              });
        });
    });
  </code></pre>
  
  <p>I get a javascript heap out of memory error:</p>
  
  <blockquote>
    <p>FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap
    out of memory</p>
  </blockquote>
  
  <p>But when I run the function a single time by passing in the path to the actual file:</p>
  
  <pre><code>openFile('./XML/2016-10-1/EUROTIPOLD2016-10-1T00-00-22.5756240530.XML')
    .then(function(data) {
      parseFile(data)
          .then(function(object) {
            console.log(object);
          });
    });
  </code></pre>
  
  <p>I get the desired output.</p>
  
  <p>What am I doing wrong?</p>