Why is the sleep duration taking so long in some cases?

3 years ago by iFarmGolems to c/javascript

Hi, I need to create a infinite (but breakable) cycle where I can slow down the cycle by awaiting promises inside. While cycle should be able to do this, but as you can see in the image, the duration is all over the place.

Why is this happening? Is there a way to make it close to the original sleep duration?

pe1uca 8 points 3 years ago

JS is a single threaded language, so there's just one long line of instructions, there's no actual background tasks.
A promise just runs at the next available slot the event loop provides so it's just as soon as it can run.

In this case I'm guessing other part of the code running in your JS engine is blocking the event loop preventing your code to run just after one second.

Where are you running it? Browser or server?
Are you using a framework? If so, have you tried just this piece of code without any other imported code?

path: 0 985745, hotness: undefined, score: 8, children: 2
pe1uca 3 points 3 years ago

Here's a talk at JSConf to help understand this

Jake Archibald on the web browser event loop, setTimeout, micro tasks, requestAnimationFrame, ...
https://youtu.be/cCOL7MC4Pl0

path: 0 985745 986021, hotness: undefined, score: 3, children: 1
charolastra 2 points 3 years ago

This talk explains everything I had always wondered about concurrency in JS, but could never be bothered to read about.

path: 0 985745 986021 1243525, hotness: undefined, score: 2, children: 0
JaddedFauceet 5 points 3 years ago

Here is the code for anyone else who want to try

async function slowLoop() {
    while (true) {
        console.time('sleepDuration')
        await new Promise(resolve => setTimeout(resolve, 1000))
        console.timeEnd('sleepDuration')
    }
}

It is not suppose to deviate too much. setTimeout will execute whenever possible at / after the given timeout duration. I am getting between 1006 to 1023 on my sysyem

Is your device performance starved during this test?

path: 0 978133, hotness: undefined, score: 5, children: 3
iFarmGolems 1 point 3 years ago

It's not. I am running process lasso tough but doubt that's the issue.

I have Ryzen 5800x3D and tried the code on latest Ms edge (chromium) browser.

Which browser did you try it on? Or did you try to run it in node?

The browser tab was active and focused also.

path: 0 978133 979177, hotness: undefined, score: 1, children: 2
JaddedFauceet 2 points 3 years ago

I have tested the code in both Chrome and MS Edge.

Is anything else running on the same page that you are testing?

You can test this in an empty environment like this: Enter this in your URL, "about:blank" and test the code in the console.

path: 0 978133 979177 980081, hotness: undefined, score: 2, children: 1
iFarmGolems 1 point 3 years ago

I tried it in about:blank but it does the same. Maybe there is some extension in my browser that slows it down (somehow). Im running tampermonkey but it's not active on any pages I tried this on.

I ran the script in nodejs and everything works perfectly, the sleep time is very close to the original value so it's definitely something inside browser that causes it to lag.

path: 0 978133 979177 980081 1024227, hotness: undefined, score: 1, children: 0
Windex007 4 points 3 years ago

I don't know anything about JS, but those timings are suspiciously close to integer multiples of your sleep duration, after the first.

Based on what you've shown, "All over place" is not what I'd call it. It either fires "on time" or takes "exactly-ish" twice as long.

path: 0 979649, hotness: undefined, score: 4, children: 1
iFarmGolems 1 point 3 years ago

I said all over the place because I've seen numbers like 1.7 seconds, 2.8 seconds and so on. But it does mostly land on integer multiples.

Anyway, tried to run this in nodejs and it works as expected - so the conclusion is that it's somehow a limitation in browser engine (V8 in this case)

path: 0 979649 1024143, hotness: undefined, score: 1, children: 0
charolastra 2 points 3 years ago

Pretty consistent defining/running directly in the console on a blank tab in Firefox

path: 0 1235442, hotness: undefined, score: 2, children: 0
masnun -1 points 3 years ago
path: 0 974290, hotness: undefined, score: -1, children: 1
iFarmGolems 1 point 3 years ago

This also happens when I create recursive asynchronous function that awaits the sleep and then itself. I have no clue what the problem is in that case.

path: 0 974290 976751, hotness: undefined, score: 1, children: 0
javascript
javascript

@programming.dev

login for more options
2830
357
51

go to feed...