Тёмный

Scheduling Tasks - HTTP 203 

Chrome for Developers
Подписаться 760 тыс.
Просмотров 22 тыс.
50% 1

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 80   
@ChromeDevs
@ChromeDevs 3 года назад
The RU-vid team is making us ask you to subscribe. Do it if you want.
@sharukh7860
@sharukh7860 3 года назад
This comment was posted 4 days ago? But the video is here for 7 minutes.
@bauxite13
@bauxite13 3 года назад
@@sharukh7860 Wait what
@cat-.-
@cat-.- 3 года назад
Ok I tried but apparently subscribing twice doesn't work!!
@rkv88kanyan39
@rkv88kanyan39 3 года назад
thank you for the JSConf talk 🙏🙏🙏
@louischa613
@louischa613 3 года назад
"The RU-vid team is making us ask you to subscribe." how come this statement sounds bogus
@Theraot
@Theraot 3 года назад
This video is a good excuse to watch Jake's talk about event loops again.
@rkv88kanyan39
@rkv88kanyan39 3 года назад
15:14 "setImmidiate" cannot run task immediately that's so sad 😁😁
@Theraot
@Theraot 3 года назад
After some reading and watching stuff, I think this is about right: Sync (call): Stop what you are doing and do this, then return to continue here. Async (await call): Stop what you are doing and do this, continue here sometime later. Task (MessageChannel): Do this soon-ish, but I don't care when. Task (setTimeout): Do this soon-ish after this much time elapsed. Microtask (queueMicrotask): Do this as soon as you are free, even if you have to block rendering to do it. Microtask (Promise-resolve-then): use queueMicrotask if available (which currently is everywhere). requestAnimationFrame: Do this just before the next rendering. setImmediate: Browser: Avoid, not portable, not standard. Use Task (or requestAnimationFrame if it makes sense). Node: Do this sometime before processing more requests. Nanotask (process.nextTick): Avoid, not portable. You probably want Microtask (you may need process.nextTick because you need ordering with other code that use it). Hot take: process.nextTick should be deprecated in favor of queueMicrotask.
@marshal7591
@marshal7591 3 года назад
Great video as always Surma and Jake!
@paulmel4411
@paulmel4411 3 года назад
Jake, you're one the brightest minds I know in web development. Don't be afraid to say I don't know. We love you. I love you.
@lotsofloops
@lotsofloops 3 года назад
I fired up IE11 to test this. In IE11 (after ES5ifying the code), setImmediate callbacks run before setTimeout tasks (as with Node), but MessageChannel tasks run before setImmediate callbacks.
@jakearchibald
@jakearchibald 3 года назад
Hah 'immediate' indeed
@halilbasmaci
@halilbasmaci 3 года назад
They are very funny hssjdhsj 🎉😂😀
@ArielVolovik
@ArielVolovik 3 года назад
Yay! More HTTP203 :D
@hypersonic12
@hypersonic12 3 года назад
I was surprised not to see mention of requestAnimationFrame. I thought that was the canonical way of scheduling a microtask nowadays.
@dassurma
@dassurma 3 года назад
It schedules a callback just before the next paint will happen in the browser. It schedules that function as a microtask at that point in time, but it’s not “the next” microtask.
@jakearchibald
@jakearchibald 3 года назад
@@dassurma WELL ACTUALLY it's not a task or a microtask, it's just a callback that happens during the rendering steps
@SimonBuchanNz
@SimonBuchanNz 3 года назад
@@dassurma raising the question of when the next paint is. I assumed it was normally simply after the last vsync, but it would be interesting to know how it deals with modern high and variable frame rate monitors. In particular, VRR means "after the last refresh" is as soon as you submit the image to the swap chain, so you could in theory starve the task queues if RAF got to (literally?) jump the queue. I imagine this was already hit with driver forced disabled vsync and solved by modern browser async rendering, though scheduling fairness probably is still a real problem.
@dassurma
@dassurma 3 года назад
@@jakearchibald dammit. Well, watch Jake’s talk everyone, he explains rAF there.
@elomoosk5991
@elomoosk5991 3 года назад
Ah yes, Rhett and Jesse Eisenberg
@abrarcalculas
@abrarcalculas 3 года назад
Is the use of MessageChannel a standard way or just some sort of "hack"? Because it's the first time I came across this and I was always under the assumption setTimeout is the "idiomatic" way of scheduling task. As for setImmediate, I think you got it right because I got similar results in small experiments I ran to verify the scheduling some time ago (because of a notorious bug I introduced in one of my active codebase while trying to be clever), although the ordering how it was affected by the use of MessageChannel is very confusing. Anyways, as always, thanks for the great content.
@mfbx9da4
@mfbx9da4 3 года назад
Great breakdown of the different task scheduling functions! It would have been really interesting if you discussed use cases for when one might prefer using a microtask over a task. For now, I just know that `queueMicrotask()` will happen first but not sure when I would use that knowledge.
@dexterous7516
@dexterous7516 3 года назад
I have used setImmediate before and the way I looked at it as it was a replacement of setTimeout with 0. In other words, I felt it is the same thing as setTimeout with 0 time and looked a bit cleaner to read and understand.
@nenadvicentic
@nenadvicentic 3 года назад
This is correct. Back in the day when only `setTimeout` existed (no `Promise`, etc), `setImmediate` was introduced as a clean/correct way for `setTimeout` with 0, which was considered a workaround.
@VivekSharma-oz1ku
@VivekSharma-oz1ku 3 года назад
This was marvelous! Thank you for making this!
@deathcore420
@deathcore420 3 года назад
in what type of projects would you use these functions?
@rcarias78
@rcarias78 3 года назад
What are the practical purposes between micro task and task. When do you use which in a browser?
@jakearchibald
@jakearchibald 3 года назад
Rendering can happen between tasks, it cannot happen between microtasks
@vivianecosta6730
@vivianecosta6730 3 года назад
After the poll phase is empty, the next phase is to check if there is any setImmediate on the queue and after that, the timers are executed. So, I guess that is why you get that result at 14:49
@winterheat
@winterheat 3 года назад
studying JS like studying atoms and quantum mechanics, in the microscopic level
@Benimation
@Benimation 3 года назад
Funny how the one with "immediate" in its name is the last one to run..
@abhinavraut3099
@abhinavraut3099 3 года назад
thanks
@1vader
@1vader 3 года назад
What's up with the "() => cb()" or even "() => { cb(); }"? Why not just pass the callback directly?
@PeerReynders
@PeerReynders 3 года назад
I suspect it's defensive programming (and being explicit). Search for "Jake Archibald: Don't use functions as callbacks unless they're designed for it" > ['1.2', '2.3', '10.11'].map(parseInt); (3) [1, NaN, 2]
@1vader
@1vader 3 года назад
@@PeerReynders I see, that makes sense, it avoids passing any arguments down to the callback.
@benoitleger-derville6986
@benoitleger-derville6986 3 года назад
Please, may you explain why microTasks are "function call dependant" and not macroTasks as I understand from this test: function tasksFromFunctionCall() { setTimeout(() => { console.log("Came from setTimeout [macroTasks]"); }, 2000); queueMicrotask(() => { Array(40000000) .fill(5) .map((e) => e + 5); console.log("Came from queueMicrotask [microTask]"); }); console.log("Came from inside function body"); return "Came from the Return function"; } tasksFromFunctionCall() ******* Result ******* Came from inside function body Came from queueMicrotask [microTask] "Came from the Return function" Came from setTimeout [macroTasks]
@jakearchibald
@jakearchibald 3 года назад
That isn't the right order. You're being misled by when the devtools console displays the return value of a function jsbin.com/nujusinaja/edit?js,console
@benoitleger-derville6986
@benoitleger-derville6986 3 года назад
​@@jakearchibald Indeed, thank you. Does this mean anything special about "the devtools console displays" for function returns? There is no specific order (rules) for this? I mean, is this devtools print "
@jakearchibald
@jakearchibald 3 года назад
@@benoitleger-derville6986 devtools will execute the code you give it, then output the return value. Executing the code ends with the JS stack emptying, which causes microtasks to be processed, so the microtasks run before the return value is displayed. The confusion happens because the return value display isn't part of the console log queue. Even in the console if you log the return value (rather than just letting devtools display it) you'll see the correct order
@benoitleger-derville6986
@benoitleger-derville6986 3 года назад
@@jakearchibald Perfect, Thank you for your time
@IceMetalPunk
@IceMetalPunk 3 года назад
Surma: "setImmediate is something that existed in the browser, but only IE, no other browser shipped it." Me: "So what you're saying is, it never existed." I try my hardest to ignore anything that IE ever did against standards, and pretend that Microsoft first got into the browser game with Edge.
@Theraot
@Theraot 3 года назад
According to caniuse this existed in early Edge but was removed. Or rather, I should say, looking at the version numbers, it was not ported to Chromium.
@IceMetalPunk
@IceMetalPunk 3 года назад
@@Theraot Let me be more specific, then: I pretend that Microsoft first got into the browser game with Chromium-Edge.
@jakearchibald
@jakearchibald 3 года назад
It's in NodeJS though
@IceMetalPunk
@IceMetalPunk 3 года назад
@@jakearchibald True. I guess what I meant is "it never existed in browsers".
@abiiranathan
@abiiranathan 3 года назад
We missed you like our life depends on http 203
@terrible_-
@terrible_- 3 года назад
love you two broskies xx
@kirkanos771
@kirkanos771 3 года назад
Did you read my paper on picotask() ?
@ColinRichardson
@ColinRichardson 3 года назад
Lets just skip all the other bull and jump straight to planktask() that is pretty much "run on the next cpu cycle", (like an interrupt in arduino)
@ann.mignon
@ann.mignon 3 года назад
The setTimeout-example looks a little "cargo-culty" with the callback wrapped in a lambda for no apparent reason. I (hopefully) imagine it's compiled away as to not make a difference, but surely the more direct setTimeout(cb, 0) is preferable for readability here? I refuse to believe a redundant anonymous function scope could possibly make a difference for event scheduling (well knock on wood, it's JavaScript).
@ann.mignon
@ann.mignon 3 года назад
@Ricardo Ferreira Sure, but the given example doesn't feature any arguments to the callback. It just seems needlessly verbose for the given example (although setTimeout is discouraged for this purpose).
@JamesCoyle95
@JamesCoyle95 3 года назад
@Ricardo Ferreira you can pass arguments directly to setTimeout after the delay parameter and they will be passed to the callback function when it is called. E.g. setTimeout(cb, 0, arg1, arg2)
@ann.mignon
@ann.mignon 3 года назад
@@JamesCoyle95 Didn't know this, may be handy, thank you.
@ann.mignon
@ann.mignon 3 года назад
@@JamesCoyle95 ofc there are some special cases where you'll need to contain certain parameters within a closure, eg. if setTimeout schedules a function call from inside a loop, passing some value dependent on the index. But I reckon that's well beside your point, comment mosdef not intended as some "gotcha"! Not to mention, anyone going about triggering async timeouts in a tight loop should probably seriously consider if it's actually what they want to be doing, maybe not the best example.
@jakearchibald
@jakearchibald 3 года назад
If it's a short callback, having it inline is more readable than having to scroll (or even, go to another file) to see the function content. Also: jakearchibald.com/2021/function-callback-risks/
@JamesCoyle95
@JamesCoyle95 3 года назад
What is an actual use case for this? Why would I ever want to schedule a task or microtask?
@victornpb
@victornpb 3 года назад
When you want to run some code, but you want to let go of the main for a bit so it can run other stuff. For example let the browser do layout and run some code after. One use case is like I have an api that you can call like drawCircle drawRectangle, but I only do the drawing on the next frame, so I everything at once after your code finishes running.
@JamesCoyle95
@JamesCoyle95 3 года назад
@@victornpb in that case I'd use requestAnimationFrame to draw immediately before the next paint.
@victornpb
@victornpb 3 года назад
@@JamesCoyle95 Exactly, but if you want something not tied to 60 times a seconds you can use one of the scheduling tasks
@victornpb
@victornpb 3 года назад
Would be useful if there was an API that you could “see” what is scheduled in each queue. Without that and or clear guideline on what needs to go where, we are just playing the highest Z-index game again. They will keep adding higher and higher priority queues... scheduleMicroTickMoreImmediatest(bc)
@jakearchibald
@jakearchibald 3 года назад
If the queue was mutable, you'd still end up with the z-index game, where everyone things their thing is 'most important', so they put it ahead of everything else.
@omri9325
@omri9325 3 года назад
@@jakearchibald But maybe a debugging API?
@jakearchibald
@jakearchibald 3 года назад
@@omri9325 a view of the queues in devtools would be nice
@SimonBuchanNz
@SimonBuchanNz 3 года назад
Seems hard to visualize? A series of queue depth graphs on a performance profile sounds the easiest.
@victornpb
@victornpb 3 года назад
@@jakearchibald where do we go about making such proposal? The ChromeDevTools GitHub org doesn’t seems to be open to public discussion, and I’m not sure if crbug.com is a suitable place.
@baizeedrony
@baizeedrony 3 года назад
Helpful
@mrala
@mrala 3 года назад
how about requestAnimationFrame?
@omri9325
@omri9325 3 года назад
It's async, but slightly different than the others, the intention is not related to other task queues but about when it's best to render animations.
@SimonBuchanNz
@SimonBuchanNz 3 года назад
It would be interesting to know if it can jump the queue on microtasks, but surely it would for others. Surely.
@tux.solbakk
@tux.solbakk 3 года назад
"oh, more dead trees", so happy to see other developers that actually care!
@FinnBrownc
@FinnBrownc 3 года назад
Trees/paper are infinitely more renewable, recyclable, and energy efficient than computers. There are more trees in the US and Europe than in 1900. The line was said with good intentions but it’s not meaningful.
@SimonBuchanNz
@SimonBuchanNz 3 года назад
In practice, the complaint is often meant that paper is really annoying
Далее
Weak JavaScript - HTTP 203
29:22
Просмотров 25 тыс.
From nothin’ to gzip - HTTP 203
29:19
Просмотров 22 тыс.
FATAL CHASE 😳 😳
00:19
Просмотров 1,5 млн
Airpod Through Glass Trick! 😱 #shorts
00:19
Просмотров 196 тыс.
WebAssembly Threads - HTTP 203
24:42
Просмотров 23 тыс.
Are SPAs better than MPAs? | HTTP 203
25:42
Просмотров 43 тыс.
Dithering - HTTP 203
21:42
Просмотров 16 тыс.
AssemblyScript - HTTP 203
28:46
Просмотров 23 тыс.
Cross-origin fetches - HTTP 203
23:42
Просмотров 39 тыс.
Realtime Chat App in React Native and AWS (Backend) 🔴
3:29:01
Top 10 performance pitfalls - HTTP 203
36:31
Просмотров 37 тыс.