Cron job vs NodeJS setInterval for optimal performance

cronnode.jsraspbian

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?

Best Answer

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.

Related Topic