Monday, 8 May 2017

NodeJS: Asynchronous map for blocking jobs

Lets take an example where I have a huge array with elements being stringified JSON. I want to iterate over this array and convert all strings to JSON using JSON.parse(which blocks the event-loop).

var arr = ["{...}", "{...}", ... ]      //input array

Here is the first approach(may keep the event loop blocked for some time):

var newArr = arr.map(function(val){
  try{
    var obj = JSON.parse(val);
    return obj;
  }
  catch(err){return {};}

});

The second approach was using async.map method(Will this improve compared to the first approach?):

var newArr = [];
async.map(arr, 
  function(val, done){
    try{
        var obj = JSON.parse(val);
        done(null, obj);
      }
      catch(err){done(null, {});}
  }, 
  function(err, results){
    if(!err) 
      newArr = results;
  }
);

If the second approach is same or almost same then what is efficient way of doing this in node.js.

I came across child processes, will this be a good approach for this problem?



via Aman Gupta

No comments:

Post a Comment