I tried making this notebook to check the difference, the values get further apart as I change focus away from the tab and back to the tab, is that relevant?
I don’t have ideas about tick vs delay, but I do have some relevant information about your last paragraph.
Every time a cell yields, the Observable runtime will wait until the next requestAnimationFrame before the cell gets another chance to execute. Most browsers will slow down requestAnimationFrame requests when the tab is not focused. So this is expected, but it probably makes your test less valid. I’d recommend keeping the tab focused while you are measuring the values.
You should call (requetAnimationFrame) whenever you’re ready to update your animation onscreen. This will request that your animation function be called before the browser performs the next repaint. The number of callbacks is usually 60 times per second, but will generally match the display refresh rate in most web browsers as per W3C recommendation…
… requestAnimationFrame() calls are paused in most browsers when running in background tabs or hidden<iframe>s in order to improve performance and battery life.
Maybe this property of requestAnimationFrame is related to the difference in Promises.tick vs Promises.delay… (for example, maybe Promises.tick will “keep going” despite the change in browser tab).
Or maybe it makes my notebook a bad test if I’m trying to understand the differences!
I’ll mark your answer correct if I don’t hear back.
The difference between the two is that Promises.tick() waits until the next multiple of your duration relative to the current time, which lets you account for variations in runtime between each yield.
In summary, I’d describe the difference inside loops as follows:
Promises.delay: Run something IN certain intervals:
If you need to process things in the background, you can try one of the many setImmediate polyfills which use the MessageChannel API under the hood. See this issue for more
Let me repeat the distinction you phrased so nicely:
And let me reiterate the parts of your answer I should’ve tried:
Checking implementation in the code of the Observable standard library itself (vs trying to check .toString() technique, or Show function definition in Chrome devtools)
Checking the Observable standard library documentation (vs trying to check only Observable tutorials)
By the way, I think the distinction “Promises.delayIN intervals, vs Promises.tickAT intervals” is what this StackOverflow answer re: recursive setTimeout vs setInterval is trying to articulate (but the SO answer is hard to follow!).
That is, the time required to do the processing “between each yield” (aka the “function takes time to process”)
In other words
recursive setTimeout is comparable to Promises.delay,
and setInterval is comparable to Promises.tick
Because: setTimeout/Promises.delay will get pushed further out in time (2,206 ms; so its incremented variable i will be eventually be lower), because it waits for the stuff “between each yield” aka “the time it takes for the function to finish”: task=123ms + delay=1000ms + task=83ms + delay=1000ms + ... = 2,206 ms
Whereas setInterval / Promises.tick will not get pushed as far out in time (2,000 ms; so its incremented value i will eventually be higher), because it *doesn’t wait for the stuff “between each yield” task=123ms + tick=(1000-123)ms + task=83ms + tick=(1000-83)ms + ... = 2,000 ms