Question

I have a beanstalkapp worker that is made with nodejs what it does is there is a PHP application which does all the site stuff and if there are errors or issues or notifications or what ever it adds it to the beanstalkapp. The nodejs then needs to run pretty much constantly checking the beanstalkapp for any messages and do something with them (email someone, add it to the log, post somewhere).

My question is, is this bad performance wise or is there a better way to do this? I would assume that setInterval doesn't let the process end and would therefore be bad?

Was it helpful?

Solution

It depends on your setInterval time. The javascript interpreter sleeps between events. Therefore, in between setIntervals your node.js app consumes zero (or almost zero) CPU time.

So how much load the app incurs on the system as a whole depends on how often setInterval fires. Once every second would hardly consume any CPU at all. On the other hand, once every 1ms (called with a setInterval time of 1 or 0) can bog down your system especially if you're running on a resource constrained machine or VM.

In my experience, a good compromise is around 50 or 100 ms (20 or 10 times per second). It's responsive enough for even real-time applications (because human perception is a lot slower than 20Hz) and long enough to have little effect on the rest of the system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top