0

I need to get 2 resultlist from a sourcelist, and write them to files.

Since the list is big, I'm trying to use tail recursion in node:

But I still get Maximum Stack error.

I'm guessing it comes from the global variables,but not sure, and I don't know how to solve it.

Thanks for your help.

`use strict`

var sourcelist = [{},{},...] //400,000 items
var resultlist1 = [];
var resultlist2 = [];
var hashSet = new Set(); //list 2 need deduplicate with hash.

function do1stJob(list, index=0){

  if(list.length==index){
    var rawdata = resultlist1.toString()

    // I'm also tried another method:
    // resultlist1.foreach(e=>rawdata=rawdata+e);

    fs.writeFileSync(destination1, rawdata, 'utf-8);
    return do2ndJob(list);
  };

  var a=list[index];
  a.addinfo = a.addsomeinfo()
  var b = do_some_calucation(a)
  resultlist1.push(b)

  index +=1;
  return do1stJob(list, index)
}


function do2ndJob(list, index=0){

  if(list.length==index){
    var rawdata = resultlist2.toString()
    fs.writeFileSync(destination2, rawdata, 'utf-8);
    return 'all done';
  };

  var a=list[index];
  a.addinfo = a.addsomeinfo()
  var hash=md5(a);
  if(hashSet.has(hash)==false){
    hashSet.add(hash)
    var b = do_some_calucation(a)
  }
  resultlist2.push(b)

  index +=1;
  return do2ndJob(list, index)
}

do1stJob(sourcelist)

kaikaiiiiiii
  • 63
  • 1
  • 7
  • Add some `console.log`s to see where it fails. Does it ever get to do2ndjob? Is the statement `if(list.length==index){` in do1stjob and do2ndjob ever `true`? How many items is there in the sourcelist? – Molda Jun 25 '20 at 08:21
  • if(list.length==index){ statement is true. It must appears if you made intial index=0 and make it +1 every recursion. Los show sometimes it fails on job 2's one recursion, sometimes in calc rawdata, if I remove some keyvalue from sourcelist – kaikaiiiiiii Jun 25 '20 at 08:37

1 Answers1

0

I found the answer here:

ES6 Tail Recursion Optimisation Stack Overflow

Node.js based on Chrome V8, and it doesn't support TCO right now. So tail recursion doesn't mean avoiding stack overflow.

kaikaiiiiiii
  • 63
  • 1
  • 7