Might be only my limited understanding but isn't that trying to recreate cooperative multitasking? That was tried, and often failed, for many years in the OS space.
Main-Thread-Scheduling
21–26 of 26 posts
Re: Main-Thread-Scheduling
#22Re: Main-Thread-Scheduling
#23The author here. Just opened HN and saw that my library is on the front page . If you have any questions, I can answer them.
Have you benchmarked the overhead of awaiting? I believe that in doing so you yield to the microtask queue and then your function has to be scheduled and run again. You could compare await yieldOrContinue('user-visible'); doSomeWork(); against if (isTimeToYield('user-visible')) { await yieldControl('user-visible'); } doSomeWork(); A few years back when I measured it on a fast laptop the cost was ~1µs to do `await 0;`
Re: Main-Thread-Scheduling
#24The author here. Just opened HN and saw that my library is on the front page . If you have any questions, I can answer them.
Hi, forgive me if I missed it, I just briefly browsed the repo. Does the library itself handle cancellation, or would user code be responsible for that?
Re: Main-Thread-Scheduling
#25Repo has a great readme.md. well written
Re: Main-Thread-Scheduling
#26Earlier quoted context omitted.
It's done — https://github.com/astoilkov/main-thread-scheduling/commit/0... . Thanks. A little thing but a great improvement. I'm now wondering why I did that.
You should do some measurement before calling it an improvement. If you read the ECMAScript standard[1] or this blog post from V8[2] about the inner workings of the `await` keyword, you would know even if you `await` a non-Promise value, it would still create a new Promise and put the suspended routine onto the microtask queue. So the change you made won't make a difference. Besides, there is a JavaScript language fe…
About the generations, do you imagine a function that accepts a generator function and the user uses yield? Something like:
``` mainThreadScheduling(function *(yieldOrContinue) { yield yieldOrContinue; }) ```