Cron job vs NodeJS setInterval for optimal performance
I'm building a simple nodejs script that updates a DNS record based on my current IP. The script works fine but I have some concerns regarding how should I run it. The check for my IP must be performed every 5 minutes and I'm facing a dilemma.
Should I use Node's setInterval or should I create a cron job? Which will consume the least RAM and CPU? Please keep in mind that the script runs on a Raspberry Pi Zero with 512MB ram and only 1 core.
I know that the cron
seems a better option but how much better is it? Does it matter given my specs?
Solution 1:
Advantages of cron solution
- less memory consumption for 90% of the time
- memory leaks are effectively eliminted
- the code is reloading all libraries every time so updates take effect on the next run without additional complexity
Advantages of daemon solution
- load time only happens once so disk I/O and CPU are lower for subsequent runs since you won't be parsing your source code or pulling in all of the libraries
- the lower CPU and disk I/O for subsequent runs also means that there is more of those resources available for whatever your main application for the Raspberry may be.
- Linux should swap out the memory you're not using. This could be really slow depending on your storage though.
Conclusion
I agree with Alexander T that cron
seems likely to be better, but if your main app is sensitive to background processes it might be better to go with the daemon.
Solution 2:
My opinion, cron
is better. I think start your script every 5 minutes is better then have started script that triggered every 5 minutes. For example, if your script dies, what you will do? With cron it just start next time in 5 minutes.
So for memory and CPU. I don't know how node.js spend memory right now, but earlier there was problem with memory leaks, and with process working for long time there could be a problem. For CPU utilization, there is no difference, I think.