astoilkov / main-thread-scheduling

Fast and consistently responsive apps using a single function call
MIT License
1.29k stars 32 forks source link

callback based API #10

Closed ronag closed 9 months ago

ronag commented 9 months ago

The performance overhead of the current Promise based API is not non-trivial. Any chance to get a callback API as well?

astoilkov commented 9 months ago

Can you tell me a little bit more?

If you use isTimeToYield() the overhead of queueing the await yieldOrContinue() for the next microtask disappears. Will that work for you?

ronag commented 9 months ago

Yea, that does improve things a bit.

astoilkov commented 9 months ago

Awesome! With isTimeToYield(), yieldControl() is called at most once per frame. However, if you experience an overhead while testing please let me know. Thanks!

if (isTimeToYield('user-visible')) {
  await yieldControl('user-visible')
}