dspows0637
dspows0637
2012-10-10 01:15
浏览 175

NodeJS setInterval对性能有害吗?

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?

图片转代码服务由CSDN问答提供 功能建议

我有一个用nodejs创建的beanstalkapp工作器它的作用是有一个PHP应用程序来完成所有站点 东西,如果有错误或问题或通知或什么,它将它添加到beanstalkapp。 然后,nodejs需要经常运行检查beanstalkapp以查找任何消息并对它们执行某些操作(给某人发送电子邮件,将其添加到日志中,发布到某处)。

我的问题是,是 这种糟糕的表现还是有更好的方法吗? 我会假设setInterval不让进程结束,因此会不好?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douji3426
    douji3426 2012-10-10 01:44
    已采纳

    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.

    点赞 评论

相关推荐