How to use worker threads in nodejs?

926 Views Asked by At

I have below code, Where passing array length is too high and processing each array element taking one second of time, How i can use worker threads in below condition?

function processData(arr){
    var result = [];
    for(var i = 0; i < arr.length; i++){
        result.push(process(arr[i]));
    }
    return result;
}

function process() {
    // some code here takes 1 second to execute
}

processData(arr);
1

There are 1 best solutions below

3
On

You can use workerpool npm package to do your long processing job in a worker thread, something like this

const workerpool = require('workerpool');

const pool =  workerpool.pool();

const arr = [...] // Large Data set which is to be processed

new Promise((resolve, reject) => {
  pool.exec(
    arr => {
      var result = [];
      for(var i = 0; i < arr.length; i++){
        /* 
        Do the long processing on data 
        */
        const processedData = a[i];
        result.push(processedData);
      }
      return result;
    },
    [arr]
  )
  .then(result => resolve(result))
  .catch(err => reject(err));
});