← Back to context

Comment by jagged-chisel

7 hours ago

I don’t think “artificially increased” is correct. See your sibling. If the runtime waits until expiry, and only then adds the task to the end of the work queue, there’s no point at which any delayed work could happen at expiry except the work to place it on the end of (an empty) queue.

Any busy runtime (e.g. one with lots of parallel tasks, plus anything running less than optimally) will have a delay.

Artificially increased is what's happening when you request a timeout of 0 and the browser always makes it 4 ms or more.

Imagine this code:

    let value = 0;

    (function tick () {
      value += 1;
      console.log(value);
      setTimeout(tick, 1);
    })();

If you let `tick()` run for 1 second, what would you expect the `value` to be? Theoretically, it should be around 1,000, because all you're doing is running a function that increments the `value` and then puts itself back onto the execution queue after a 1 ms delay. But because of the 4 ms delay that browsers implement, you'll never see `value` go above 250, because the delay is being artificially increased.