Best way to retrieve music metadata from audio files? I am using the music metadata-browser npm package to retrieve audio metadata from files. This library is being used in an Electron and React desktop app. 

To get the metadata of audio files, (and add it to the redux state), I use the following function to get metadata and add the file to redux state. This method works well with small amounts of data, but gets really slow (obviously) as more audio files are given to be processed. Is there a better way I can process these files? Not sure if javascript has any worker/job techniques I could use.

import * as mm from 'music-metadata-browser';

const addFile = (filePath, file, dispatch, ext) =&gt; {
  if (Buffer.isBuffer(file)) {
    mm.parseBuffer(file, ext)
      .then(metadata =&gt; {
        const libraryEntry = createLibraryEntry(filePath, metadata);

        dispatch({ type: constants.ADD_FILE, libraryEntry, totalFiles });
      });
  }
  else {
    mm.parseBlob(file, ext)
      .then((metadata) =&gt; {
        const libraryEntry = createLibraryEntry(filePath, metadata);

        dispatch({ type: constants.ADD_FILE, libraryEntry, totalFiles });
      });
  }
};

const processFiles = (files, dirPath, dispatch) =&gt; {
  files.map((file, index) =&gt; {
    const parsedFile = file.split('.');
    const format = parsedFile[parsedFile.length - 1];
    const filePath = `${dirPath}/${file}`;
    const isDirectory = fs.lstatSync(filePath).isDirectory();
    if (isDirectory) {
      fs.readdir(filePath, (err, files) =&gt; {processFiles(files, filePath, dispatch);});
    }
    else if (isValidFormat(format)) {
      fs.readFile(filePath, (err, file) =&gt; {
        addFile(filePath, Buffer(file), dispatch, format);
      });
    }
    return;
  });
};


Thanks for the help.
It appears music-metadata is the Node.js/desktop flavor of music-metadata-browser (intended for browsers).  The author doesn't explain the differences between the two, but maybe the desktop variant will be faster for you.

Synchronous anything is a mistake for an inherently parallel task like this.   Use fs.lstat instead of the fs.lstatSync.  If createLibraryEntry or dispatch touch the disk they should be replaced with async versions as well.

Worker threads are a thing but you probably don't need them, because disk access is what's slowing you down and async disk access doesn't require threads.  

If you have disk I/O that can't be asynchronized (if that's a word) by conventional callback functions, or if you've made everything async and it's still too slow, that is the time to consider using workers.