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)