javascript setTimeout() 会影响性能吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19578446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:01:50  来源:igfitidea点击:

does setTimeout() affects performance

javascript

提问by Matt Welander

I wonder if this code is going to put load on the client since the timeout is so long?

我想知道由于超时时间太长,这段代码是否会给客户端带来负载?

        //and update this again in a bit
        setTimeout(function() {
            updateWeather(lat,lng);
        }, 60000);

采纳答案by bobbybee

Not that code alone. The system idles for that one minute. As long as updateWeather doesn't have severe performance issues and the interval is short, setTimeout won't be a product (and I believe you mean setInterval, not setTimeout for recurring checks)

不仅仅是那个代码。系统空闲那一分钟。只要 updateWeather 没有严重的性能问题并且间隔很短,setTimeout 就不会是一个产品(我相信你的意思是 setInterval,而不是用于重复检查的 setTimeout)

回答by Darren Cook

The 60 second timer is implemented by magic in the OS: it basically adds no CPU load during the 60 seconds it is waiting.

60 秒计时器在操作系统中是通过魔法实现的:它在等待的 60 秒内基本上不增加 CPU 负载。

I guess updateWeather()is polling an external resource, so the answer to your question is a simple "no, it is fine". (As the weather does not change thatoften, I'd make it 5 minutes instead, see comment below on battery life.) (Even better: see if the weather data provider gives you a field telling you when the next update will be, and use a setTimeoutbased on that.)

我想updateWeather()是轮询外部资源,所以你的问题的答案是一个简单的“不,很好”。(由于天气没有改变的是经常,我会让它5分钟代替,请参见下面的电池寿命评论。)(更妙的是:看,如果天气数据提供给你一个场告诉你下次更新将是,并setTimeout在此基础上使用一个。)

In other situations, for instance if you have been collecting some kind of data for those 60 seconds, and then go and process it in one go, this could cause a peak of heavy load. Which might be noticed by the user (e.g. all animations go jerky once every 60 seconds). In that case it is better to use a 5 second timer, and process 5 seconds worth of data at a time.

在其他情况下,例如,如果您在这 60 秒内一直在收集某种数据,然后一次性进行处理,这可能会导致重负载高峰。用户可能会注意到这一点(例如,所有动画每 60 秒就会出现一次抖动)。在这种情况下,最好使用 5 秒计时器,并一次处理 5 秒的数据。

Conversely, if you are using the network on a mobile device, you have to consider battery life: wake-up-and-find-a-connection can dominate. So extending a polling time from 60 seconds to 120 seconds can literally double battery life. Ilya Grigorik has a very good chapter on this in his book, High Performance Browser Networking, http://shop.oreilly.com/product/0636920028048.do...here it is: http://chimera.labs.oreilly.com/books/1230000000545/ch08.html#ELIMINATE_POLLING

相反,如果您在移动设备上使用网络,则必须考虑电池寿命:唤醒和查找连接可能占主导地位。因此,将轮询时间从 60 秒延长到 120 秒实际上可以使电池寿命加倍。伊利亚·格里戈里克对此有一个很好的章节在他的书中,高性能浏览器网络,http://shop.oreilly.com/product/0636920028048.do......这里是:HTTP://chimera.labs.oreilly。 com/books/1230000000545/ch08.html#ELIMINATE_POLLING

回答by Gautam Bhutani

The answer to your question depends on:

您的问题的答案取决于:

  1. The device you execute this on
  2. The environment - how many times do you execute this
  1. 您执行此操作的设备
  2. 环境 - 你执行多少次

A standalone setTimeout would not hurt the browser; however, if you are doing the same thing repeatedly, it would make a difference.

独立的 setTimeout 不会伤害浏览器;然而,如果你重复做同样的事情,就会有所不同。

Have a look at the following website for more insights...

请查看以下网站以获取更多见解...

http://ejohn.org/blog/analyzing-timer-performance/

http://ejohn.org/blog/analyzing-timer-performance/

回答by papiro

One thing to keep in mind, in addition to the other answers, is the affect that the closure of the environment variables might have on memory performance. If you've got a gajillion objects being referenced by variables in the scope(s) above your setTimeoutcallback, those objects will live as long as the callback is queued. That is not something we can tell from the code you posted and so no answer can be definitively given to your question. Also, if your setTimeoutis being called multiple times, it will create a closure for each one, effectively duplicating the environment and taking up your heap space.

除了其他答案之外,要记住的一件事是环境变量的关闭可能对内存性能产生影响。如果setTimeout回调上方的范围内的变量引用了 gajillion 对象,则只要回调排队,这些对象就会存在。这不是我们从您发布的代码中可以看出的,因此无法明确回答您的问题。此外,如果您setTimeout被多次调用,它将为每个调用创建一个闭包,从而有效地复制环境并占用您的堆空间。