Reputation: 1119
Sometimes i amaze myself by writting a piece of code which can be worst performant like the one below i guess. Code simply reads all files (files are text only) in directory and prints each line of each file on console after interval of one 1/10 thsecond. Now with below approach , if there are 1 million lines in the files , there will be 1 million setTimeout functions defined. and every 1/10th second one setTimeout will call its respective function. I was just curious how below code will impact the performance ? Is it ok to have millions of callbacks defined in nodejs env ? what are your thoughts about below piece of code.
function scanDir(dir){
fs.readdir(dir , function(err , list){
var interval = 0;
list.forEach(function(file, index){
lineReader.eachLine(dir + "/" + file, function(line, last) {
interval += 100;
(function(line){
setTimeout(function(){
console.log(line+"\n\r");
},interval)
})(line);
if (last) {
return false; // stop reading
}
});
})
})
}
Upvotes: 0
Views: 141
Reputation: 25555
You can't have millions of callbacks defined, you'll run out of space on the call stack after about 12,000 or so. This is the error that you'll get:
RangeError: Maximum call stack size exceeded
Now, you can increase the call stack size if you really need to, by using the following command line argument, but I don't think it would ever support millions of callbacks.
node --max-stack-size=val
Without really understanding what you are trying to accomplish, it's hard to suggest additional improvements in your code. If you really have a million lines, printing out the lines is going to take more than a day at 1 every 1/10 sec, why would you want to do this?
Upvotes: 1