Answer the question
In order to leave comments, you need to log in
Why does setInterval eat more CPU than setTimeout?
Hello community.
I came across an extremely interesting situation in one of the modules. I'll put it in a short format ...
Code option 1:
const timer = setInterval(() => {
periodicFunction();
}, 1000);
periodicFunction() {
...рабочий код функции...
};
periodicFunction();
periodicFunction() {
const timer = setTimeout(() => {
clearTimeout(timer);
periodicFunction();
}, 1000);
...рабочий код функции...
};
Answer the question
In order to leave comments, you need to log in
After the update, I don’t know which account NODE stumbled upon old test scripts that emulated the problem. I decided to repeat the tests - the problem is not reproduced. Apparently the "root of evil" was in a specific version ...
Put console.log( counter++ )
in your periodicFunction
, see how many times it is actually called. setTimeout
and setInterval
do not eat the processor by themselves, especially up to 40%.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question