Is setInterval CPU intensive?

I read somewhere that setInterval has processor intensity. I created a script that uses setInterval and controlled the CPU usage, but didn't notice the changes. I want to know if there is something that I missed.

What the code does is check for hash changes in the URL (content after #) every 100 milliseconds, and if it has changed, load the page using AJAX. If it has not changed, nothing happens. There would be problems with the CPU with this.

+46
javascript ajax cpu setinterval
Jul 11 '11 at 12:33
source share
6 answers

I do not think that setInterval inherently will cause serious performance problems. I suspect that reputation may come from an earlier era when processors were less powerful.

However, there are ways to improve performance, and it is probably wise to execute them:

  • Pass the setInterval function, not a string.
  • Set as few intervals as possible.
  • Make the interval duration as long as possible.
  • Always execute the code as short and simple as possible.

Do not optimize prematurely - do not make life difficult for yourself when there are no problems.

However, in your particular case, you can use the onhashchange event, rather than timeouts, in browsers that support it.

+46
Jul 11 2018-11-11T00:
source share

I would say that the opposite is true. Using setTimeout and setInterval correctly can dramatically reduce browser usage by browsers. For example, using setTimeout instead of using a for or while will not only reduce CPU usage, but also ensure that the browser is able to update the UI queue more often. Long processes do not freeze or block the user's work.

But overall, using setInterval really very similar to your site, it can slow down. 20 simultaneous work intervals with more or less hard work will affect the show. And again ... you can really mess up any part, I think this is not a setInterval problem.

.. and by the way, you do not need to check the hash. There are events for this:

 onhashchange 

will work when the hash changes.

 window.addEventListener('hashchange', function(e) { console.log('hash changed, yay!'); }, false); 
+13
Jul 11 2018-11-11T00:
source share

No, setInterval not an intensive CPU. If you have many intervals that run on very short cycles (or a very complicated operation that runs on a moderately long interval), then this can easily become CPU intensive, depending on what your intervals do and how often they do it.

I would not expect to see any problems with checking the URL every 100 milliseconds with an interval, although I personally would increase the interval to 250 milliseconds, simply because I did not expect the difference between them to be noticeable to the average user and because I I usually try to use the longest latency intervals that I think I can avoid, especially for things that are expected to be absent in most cases.

+9
Jul 11 '11 at 12:40
source share

There is a little marketing going there under the term "intensive processor". In fact, this means "a more intensive processor than some alternatives." This is not an "intensive processor", as in "uses a lot of processor power, like a game or compression algorithm."

Explanation:

Once the browser has completed control, it relies on interruption from the underlying operating system and equipment to gain control and issue a JavaScript callback. Having a longer duration between these interrupts allows the hardware to enter low power states that significantly reduce energy consumption. By default, the Microsoft Windows operating system and Intel processors use a resolution of 15.6 ms (64 interrupts per second) for these interrupts. This allows Intel-based processors to enter the lowest power state. For this reason, web developers have traditionally only managed to achieve 64 callbacks per second using setTimeout (0) using HTML4 browsers, including earlier editions of Internet Explorer and Mozilla Firefox.

Over the past two years, browsers have been trying to increase the number of callbacks per second that JavaScript developers can receive through the setTimeout and setInterval APIs, changing the consciousness of the power of Windows system settings and preventing equipment from entering low energy states. The HTML5 specification has gone to extremes recommending 250 callbacks per second. This high frequency can increase energy consumption by up to 40%, affecting battery life, running costs and the environment. In addition, this approach does not address the core issue of processor performance and scheduling.

From http://ie.microsoft.com/testdrive/Performance/setImmediateSorting/Default.html

+4
Jul 11 '11 at 12:40
source share

In your case there will be no problems. But if you make huge animations in the canvas or work with webgl, then there will be problems with the processor, so for this you can use requestAnimationFrame.

Send Link About requestAnimationFrame

+3
Jul 11 '11 at 12:41
source share

Function time> interval time is bad, you cannot know when cpu hiccups is slow, and it stacks on top of the current functions until pc hangs. Use setimeout, or even better, process.nextick using the callback inside settimeout.

+1
Nov 25 '16 at 16:10
source share



All Articles