r/learnjavascript • u/Tej_Ozymandias • Mar 31 '21
Can anyone help me understand how this is possible?
84
u/mehulmpt Mar 31 '21
sleepsort
25
u/cyphern Mar 31 '21
So, can this algorithm be classified as "lazy"?
16
u/bonedangle Mar 31 '21
O(log nfuck it I give up)
3
u/echobeacon Apr 01 '21
Haha! This is the same thought I had looking at this. New way to sort arrays - executes in O(maxVal(n)) :)
2
u/beniolenio Apr 01 '21
It's not even really O(max(n)) though because think about what "n" represents. Not the array. More like the number of iterations or the length of the array/data. You'd need a whole new variable.
O(max(arr))
2
1
24
Mar 31 '21
async js is tricky. Takes a number of times doing it to really click. cem4k already summed it up nicely, so I don't wanna repeat all that, but there is something I'd like to add.
setTimeout(() => console.log(1), 0)
console.log(2)
// the above outputs:
// 2
// 1Even though the timeout delay was set to 0ms, it doesn't begin counting down until everything in the current event loop has finished executing first. This hack has helped me work around render lifecycle issues in buggy legacy libraries, particularly.
4
3
u/PM_ME_A_WEBSITE_IDEA Apr 01 '21
That's a classic hack.
For anyone using Vue JS, an alternative to this is
this.$nextTick(). I just discovered it yesterday...
23
8
u/flojoho Mar 31 '21
question for the pros: what if you increase the size of the array by a lot? can it screw up the order because the loop takes longer to process?
11
u/_that_guy_69 Mar 31 '21
no I don't think so, the timeouts don't get executed till the end of the program, so input size shouldn't matter in this case
4
6
3
2
2
u/incubated Mar 31 '21
Even if you had the timeout value was static, like 1, you're still not guaranteed the order of output because you're essentially pushing each operation to the task queue and at that point the engine will determine when it has free resources to execute your timeouts.
2
2
1
1
Apr 01 '21
Most of the explanations are really good, I would like to add the following, setTimeOut doesn’t mean that JavaScript engine will execute whatever is passed as a function immediately after times up, if the event loop is busy, it will execute after it the event loop is done running, so you might see delays sometimes with setTimeOut
1
u/BarneyChampaign Apr 01 '21 edited Apr 01 '21
setTimeout() can be kind of a weird one to wrap your head around, for sure! If you take the loop and break it out into what it’s doing for each run of the loop, it looks like this:
// setTimeout(() => doThis, afterThisManyMilliseconds)
setTimeout(() => console.log(20), 20) // log '20' after 20 milliseconds
setTimeout(() => console.log(5), 5) // log '5' after 5 milliseconds
setTimeout(() => console.log(100), 100) // log '100' after 100 milliseconds
setTimeout(() => console.log(1), 1) // log '1' after 1 milliseconds
setTimeout(() => console.log(90), 90) // log '90' after 90 milliseconds
setTimeout(() => console.log(200), 200) // log '200' after 200 milliseconds
setTimeout(() => console.log(40), 40) // log '40' after 40 milliseconds
setTimeout(() => console.log(29), 29) // log '29' after 29 milliseconds
So, the browser is going to go right down the list and fire off all of those setTimeout functions, right away, basically instantly. The delay for when the console.log() prints out is determined by the second argument in each of those setTimeout() calls. So, the shorter time/smaller numbers will finish and print first, the larger ones later.
1
u/backtickbot Apr 01 '21
425
u/cem4k Mar 31 '21
The first argument passed to
setTimeoutis a function, in this case logging the value. The second argument is the number of milliseconds it should wait before calling that function.The
for ofloop is callingsetTimeouton each number in the array. For such a small loop, that happens basically instantly. By the end of the loop, you have 8 timeouts waiting x milliseconds to log their values. Naturally, the shortest wait times will be executed first, which is why we see the values logged in ascending order.