I was working on a project with JavaScript and I was wondering if I used setInterval()
and set it to run each millisecond, would that cause performance issues? Would calling the function whenever needed be better performance wise? Or setInterval()
doesn't affect the performance?
Asked
Active
Viewed 34 times
2

Skelelele
- 21
- 4
-
2Browsers won't run it that fast anyway. Usually the shortest interval possible is around 15 or 16ms. – Pointy Feb 13 '17 at 16:04
-
1Depends on how expensive the callback is. – Damon Feb 13 '17 at 16:06
-
See also [this](http://stackoverflow.com/questions/7648557/setinterval-behaviour-with-0-milliseconds-in-javascript) SO question. – Jeroen Heier Feb 13 '17 at 16:10
1 Answers
1
if you do setInterval , your function will be added to the stack each x ms. That doesn't mean that it will be executed each x ms. Indeed the stack may not be empty at the moment the function is added to it. So you will run your function with a period which is at least x ms (no precision guaranteed!). That's the only performance issue I know about setInterval Hope that helped!

Fanyo SILIADIN
- 802
- 5
- 11