Live data from Hacker News

Main-Thread-Scheduling

github.com

21–26 of 26 posts

Re: Main-Thread-Scheduling

#21

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.

Sadly in the browser you have no choice but to perform cooperative multitasking, since the execution model prevents the browser from performing preemptive multitasking.

Re: Main-Thread-Scheduling

#23
post #14

The 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;`

I haven't. I should do that. Many people are talking about this and this makes me think I should measure and talk about it in the readme. Thanks!

Re: Main-Thread-Scheduling

#24
post #17

The 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?

This is on my list. I want to provide a `signal` property to enable easier and more efficient cancellation.

Re: Main-Thread-Scheduling

#26
post #16

Earlier 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…

Yeah, you are right. I reverted the commit.

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; }) ```

Post reply on HN