SetInterval Iterations runtime increases

Viewed 37

I try to run my script forever in node.js, but I think something ist going wrong and I don't know what to do. I run my script every one second:

async function main() {
  async function func() {
    var start_time = performance.now();
    for (let route of routes) {
      var result_amount = await calc(route);
      if (result_amount[5] > amount_start * 1.0) {
          //Do something
      }
    }
    var end_time = performance.now();
    var total_time = Math.round(end_time - start_time) / 1000;
    console.log("Time for one iteration: ", total_time);
  }
  const POLLING_INTERVAL = 1000;

  func();
  setInterval(func, POLLING_INTERVAL);
}

main().catch((error) => {
  console.error(error);
  process.exit(1); //Oder cl eine reconnection function??
});

The time for one iteration increases very fast. It starts like this:

Time for one iteration:  58.164
Time for one iteration:  73.696
Time for one iteration:  71.65
Time for one iteration:  87.609
Time for one iteration:  79.979
Time for one iteration:  81.445
Time for one iteration:  105.547
Time for one iteration:  102.197
Time for one iteration:  124.212

And after 15 minutes it is like this:

Time for one iteration:  861.125
Time for one iteration:  692.99
Time for one iteration:  880.155
Time for one iteration:  844.738
Time for one iteration:  839.594
Time for one iteration:  1019.032

Can someone tell me why this does happen? I understand, that it increases at the beginning because more iterations are running at the same time. Every one sec a new one starts and the first one needs 60 seconds, so I expect 60 running at the same time and the amount stays the same because one finishes and another one starts. Why does it not work as I thought?

THANKS!

0 Answers
Related