Managing The Event Loop Phases ⭕️ | OPTIMIZING NODE JS

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • In this first video of my "Node JS Performance Optimizations" series, I show you how to level up you Node JS skills by managing the Event Loop phases the right way. I provide simple, yet in depth explanations and examples of how the Event Loop works as well as how to best interact with it to increase app performance.
    TITLE: MANAGING THE EVENT LOOP PHASES
    NOTE: This video is suitable for beginners as well as advanced Node JS practitioners.
    Node JS Performance Optimizations TH-cam Playlist:
    • Managing The Event Loo...
    Subscribe to my Bleeding Code TH-cam Channel here:
    / @bleedingcode
    Bleeding Code on Social Media
    ==========================
    bleedingcode.com
    / bleedcode
    / bleedingcode

ความคิดเห็น • 152

  • @BleedingCode
    @BleedingCode  4 ปีที่แล้ว +5

    Hello all 👋. Question: How many of you are starting to refactor your code to make use of Promises?

  • @sanjivgautam4416
    @sanjivgautam4416 2 ปีที่แล้ว +3

    There we have it. Somebody is teaching advance concept of NodeJS. WOW. Loved your tutorial. I do not usually like any videos on youtube, but yours are just WOW

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Thanks so much Sanjiv. I appreciate that 🙏

  • @AmeyBikram
    @AmeyBikram 2 ปีที่แล้ว +8

    To be honest I bought multiple udemy courses in order to understand the internal working of event loop but I wasn't able to grasp the phases until I found your video. I went through your channel and found that it has been 1 year since you made any video related to NodeJs. Please don't stop posting videos and continue this series.

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว

      Hi Amey. Thanks so much for that feedback. I would love to post more content like this, but it depends on what I'm working on. And right now, I'm working on a lot of Web3 stuff, but still NodeJS related, just not so performance related. But a lot can change in a short time :)

  • @mostafatarekadam8861
    @mostafatarekadam8861 ปีที่แล้ว +5

    Sir I don't believe that anyone could've explained that topic the way you did, I understood pretty seamlessly
    thank you ❤❤❤❤

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว +1

      I appreciate that very much Mostafa 🙏

  • @TheDb11235
    @TheDb11235 3 ปีที่แล้ว +6

    This channel is gold.
    Loved this “theory” of the event loop. Would be very interested in seeing the “practice” - ie, some specific non-contrived examples where replacing promises with setImmediate made a material difference (and how to recognize those cases)

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Thank you Duncan. I’ve got so much content still planned but will definitely keep your suggestion in mind 👍

  • @sillycoder9690
    @sillycoder9690 2 ปีที่แล้ว +2

    Man, you explain so simply and clearly! So much better for me to understand than those "Javascript Gurus" out there! I expecially like how you point things out right from the beginning.
    Very good work and lots of thanks from us all !!!

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Thank you for that awesome feedback 🙏

  • @AdityaFingerstyle
    @AdityaFingerstyle 4 ปีที่แล้ว +11

    Loved it ! More contents on the inner workings of JS please ... 🙂

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว +1

      Thank you. Definitely more to come 👍.

  • @ravipayasi6875
    @ravipayasi6875 ปีที่แล้ว +3

    Best explanation I found till now, even better than official node Js channel explanation, liked and subbed 👍🏻👍🏻

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว +1

      Brilliant Ravi. I'm glad I could help 👍

  • @pawansuthar6709
    @pawansuthar6709 19 วันที่ผ่านมา +1

    Thank you for explaining it in a very practical way.

  • @sahrukhjethra1505
    @sahrukhjethra1505 ปีที่แล้ว +2

    I really learned event loop concept in actual way which I wanted. Thanks. keep posting more videos about how we can write non-blocking code in nodejs.

  • @rb3198
    @rb3198 4 หลายเดือนก่อน +1

    THE best video on the Event loop. Thank you so much!

  • @biplobmanna
    @biplobmanna ปีที่แล้ว +1

    Finally it all clicked in my head. Immense gratitude to you for your organized explanation.

  • @coderllama5302
    @coderllama5302 3 ปีที่แล้ว +3

    Today I found this gem of a channel :)

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      Awesome thanks. I’m glad you finding these videos so valuable 👏

  • @radrianm5
    @radrianm5 2 ปีที่แล้ว +2

    This is very nice, i finally get a clear picture about what is the event loop. Thankyou!

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Awesome I'm glad the video helped 👏

  • @nimethnimdinu9990
    @nimethnimdinu9990 2 ปีที่แล้ว +3

    wow superb❤❤

  • @rohitj1252
    @rohitj1252 ปีที่แล้ว +2

    Thanks for this vedio.
    confusion gone, I also read doc after this.

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว

      Excellent. I'm glad you came right. Apologies for the delay in responding.

  • @ProMakerDev
    @ProMakerDev 3 ปีที่แล้ว +2

    that video really really really helped me, thanks a lot, you saved me so much time, keep posting about the internals of javascript/nodejs, and keep up posting thanks

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Excellent I'm glad it helped 💪

  • @becbelk
    @becbelk 9 หลายเดือนก่อน +2

    . your video is one of the fewer very interesting. cuz it deep dive in hardware...

    • @BleedingCode
      @BleedingCode  6 หลายเดือนก่อน

      I'm really glad it added value 👏

  • @GPT-X938
    @GPT-X938 ปีที่แล้ว +1

    What a fantastic informative video. Filled with helpful information at every moment. Thank you!

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว

      You're very welcome. Thank you for the comment.

  • @khanusama7314
    @khanusama7314 3 ปีที่แล้ว +2

    Very interesting video I ever seen for event loop

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      Thank you Khan. I trust it was insightful 👍

  • @rvijayakumar
    @rvijayakumar 3 ปีที่แล้ว +1

    Great One. Gave a clear picture of how these phases are working. 👏👍 Expecting more on the core concepts.

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      Thank you Discrete Soul 🙏

  • @adigupta857
    @adigupta857 3 ปีที่แล้ว +1

    Loved this one !!! Now waiting for your worker threads video series which you promised to publish before. ❤️❤️

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +2

      Hahaha yes it's amazing how long it's taking. Life can sometimes really get in the way. However the scripts for the videos are now done. I've actually got 2 videos scheduled on Worker Threads.

  • @kid_kulafu_1727
    @kid_kulafu_1727 4 ปีที่แล้ว +2

    Wow ive read that in their documentation but never really understood it. the process.tick was confusing on nodejs documentation. I just watch your video twice and I know the gist already. You need to join the nodejs foundation sir. I think they are looking for a mentor to mentee some newbies to contribute in nodejs codebase. Thank you for this!

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว

      Mr Raphael. Thank you for this awesome comment and feedback. I'll definitely be keen to help the foundation in anyway that I can. Right now I'm just trying to get my next set of videos published 😎.

  • @guilhermerenkel3552
    @guilhermerenkel3552 ปีที่แล้ว +1

    Really good explanation! Thanks for sharing this content with us!

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว

      Glad you enjoyed it :) - John

  • @divyanshpurohit4556
    @divyanshpurohit4556 3 ปีที่แล้ว +1

    This is an amazing video !!

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Thank you Divyansh. I appreciate it 🙏

  • @kornelijussliubauskas8299
    @kornelijussliubauskas8299 2 ปีที่แล้ว +1

    Nice!!! Btw loved your metal playlist 😃🎧

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Haha thanks. I have not given it the attention it deserves :(

  • @boddulasrikanth6047
    @boddulasrikanth6047 3 ปีที่แล้ว +1

    Thanks dude....waiting for more videos

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Welcome 🙂. More coming soon

  • @sandeepreddy6247
    @sandeepreddy6247 2 ปีที่แล้ว +1

    simply Masterclass......... thanq u

  • @pisuyeon
    @pisuyeon 2 ปีที่แล้ว +1

    Very helful!! I can fully understand it. Thanks

  • @felipesales1878
    @felipesales1878 ปีที่แล้ว +1

    Awesome content, thank you!! 🙏

  • @mackkaputo8989
    @mackkaputo8989 3 ปีที่แล้ว +1

    Great explanation.

  • @ayushjain7658
    @ayushjain7658 2 ปีที่แล้ว +1

    GREAT WORK

  • @curtism7302
    @curtism7302 4 ปีที่แล้ว +1

    This was great , Now I am going to watch your other videos.

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว

      Thank you very much for the compliment. I apologize in advance for some of my initial videos. That been said I'm hoping to publish new content weekly 👍.

  • @Himanshumeenamnnit
    @Himanshumeenamnnit 3 ปีที่แล้ว +1

    nice crisp and to the point
    thanks for this awesome video

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      You’re very welcome Himanshu 🙏👍

  • @prasadbamane7589
    @prasadbamane7589 2 ปีที่แล้ว +1

    really awesome video
    thanks a lot !!!!!!!!!!!!!
    more videos pls :)

  • @KavyaThota-jg1gj
    @KavyaThota-jg1gj 3 หลายเดือนก่อน +1

    well explained

  • @balamuruganramachandran5407
    @balamuruganramachandran5407 3 ปีที่แล้ว +1

    Love you master from India

  • @vasurangpariya8492
    @vasurangpariya8492 2 ปีที่แล้ว +1

    Very useful video 👍🏻

  • @mrbiditY
    @mrbiditY 4 ปีที่แล้ว +1

    Best video so far on the Node event loop! Can you please write a very simple example here (or in a codesandbox) for an express endpoint logic that could block requests (delay the polling phase) vs a snippet that solves that same problem but doesn't block it or blocks it way less? Cheers

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว +1

      Hi Mátyás. Thanks for the reply and compliment 🙏. I've been asked questions similar to yours.
      I have a great idea for an open src app that will demo various forms of event blocking logic and how it will affect new incoming requests. The app will then have recommended alternatives for each test that will also be measured by benchmarking tools (most likely autocannon). My goal is to have this app available on GitHub very soon, of which I will then provide video tutorials on how to use the app as well as extend it.
      I might need a week or 2 to get this going, but will make it a priority 👍.

    • @mrbiditY
      @mrbiditY 4 ปีที่แล้ว +1

      @@BleedingCode That sounds very nice! Will give you a star for sure when it's done! :D

  • @MrTejomay
    @MrTejomay 3 ปีที่แล้ว +1

    Great content. Keep creating awesome contents.

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Thanks again Tejomay. Hoping to get some more videos out soon 👍

  • @orhanors1800
    @orhanors1800 3 ปีที่แล้ว +1

    Thanks for this clear explanation. You should keep it up

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      Thank you Orhan for watching. I'm glad it helped you 👍

  • @fame_o6442
    @fame_o6442 2 ปีที่แล้ว +1

    Thanks alot man...that really helped

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว +1

      I'm really glad, thank you :)

  • @balamuruganramachandran5407
    @balamuruganramachandran5407 3 ปีที่แล้ว +1

    Please post more videos master ..

  • @moazzammushtaq3606
    @moazzammushtaq3606 ปีที่แล้ว +1

    Amazing video if u have to get in deep for eventloop

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว

      Thank you I appreciate it 🙏 - John

  • @mroobert
    @mroobert 3 ปีที่แล้ว +1

    Super explanation, super content. Keep going! Subscribed.

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Awesome thanks so much for joining 🙂

    • @mroobert
      @mroobert 3 ปีที่แล้ว +1

      @@BleedingCode I'll much appreciate if you could help me with this:
      1. In your performance tips list, you have TIP NR 3 and TIP NR 5 that are related and from what I understood it prevents blocking the EVENT LOOP by not defining each async task as a micro-task. Do you have some examples in a day to day coding activity, where we could block EVENT LOOP by not respecting those two tips?
      2. From my experience with node is that almost each 3rd party dependency that you use in your app it's promised based. How this relates to TIP NR 3 and TIP NR 5.
      THX and have a nice day!

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      @@mroobert Hi Robert. Right, so for question 1: I usually pick on bcrpt-node for slow transactions because it's written completely in NodeJS and is synchronous. Another easy way is to just create a loop that iterates a few billion times. In the real world though, what usually happens is e.g. you'll fetch data from 2 separate database servers and then loop through the data to create a single object. This could really hurt the event loop.
      For question 2: For 3rd party modules that wrap their logic in promises, it's actually irrelevant and can be treated as executed immediately. If you call these promises via setTimeout and setImmediate, you will move to the next tick of event loop, thus first processing incoming requests.
      I hope I helped explain things?

    • @mroobert
      @mroobert 3 ปีที่แล้ว

      @@BleedingCode thx so much for your quick response. So by having these two answers in my mind and by reading some extra info, if I understood correctly:
      1. It is best to keep an eye on the time complexity of the callbacks. In other words, each of your callbacks should complete quickly. As you said regexps and JSON operations are a RED flag so you should be very carefully with them.
      2. setImmediate() seems to be a good choice for partitioning long-running synchronous code because it's after the Polling Phase so we don't block incoming requests. A more extreme solution is to offload potentially blocking code to a worker thread.
      3. Going back to my question with "3rd party modules that wrap logic in promises", if we take mongoose that provides you communication with a MongoDb, we can see that queries to the db are wrapped with promises. And I was curious why is done like this and not with setImmediate(). As I was thinking about a code sequence where we have an endpoint "/orders" with a callback in which we query the "orders" from the MongoDb (by promise), I was ohhh... when the callback for this endpoint gets resolved, it would be best that the callback for the "orders" db query to be resolved as soon as possible and the endpoint response to be sent afterwards. This could be done by the promise because being a micro-task, the callback attached can run in any phase in which the Event loop would be found at the moment of the resolved promise.
      Waiting for your feedback on these one:D. Thx so much for your time! All the best to you!

  • @abinashgupta4046
    @abinashgupta4046 2 ปีที่แล้ว +1

    Very helpful Content! Liked and Subscribed! Why don't you post videos more often?

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Thanks so much for the feedback. Honestly it comes down to what pays the bills right now takes priority, and my current responsibilities are taking up all my time :(. I hope to have things change in the near future.

  • @simplify9999
    @simplify9999 3 ปีที่แล้ว +1

    Pretty good,thanks.

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      You’re welcome. Thanks for watching 👍

  • @hazemabdelalim5189
    @hazemabdelalim5189 3 ปีที่แล้ว +1

    You are the best

  • @Dude29
    @Dude29 4 ปีที่แล้ว +1

    Great video full of valuable information! I also love your accent ahah.
    Do you have the code used in the video published anywhere? I would like to try it myself

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว +1

      João. Firstly, kudos on the name 👍. As I'm sure you've guessed from my last name my name is also João (from Portuguese descent), but here in South Africa it's a bit too complex, so I resort to John.
      Secondly, thank you for the compliment. I really appreciate it. Regarding the source code, gimme a day and I'll send it up to GitHub and will reply to your message with a link.
      Cheers 😎

    • @Dude29
      @Dude29 4 ปีที่แล้ว +1

      @@BleedingCode Oh wow, I didnt know that! Thank you so much!

  • @MayurPatel-ny4ue
    @MayurPatel-ny4ue 3 ปีที่แล้ว +1

    Thank you so much sir.

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      You're most welcome Mayur 🙂

  • @KavyaThota-jg1gj
    @KavyaThota-jg1gj 3 หลายเดือนก่อน

    great video

  • @dannahari3918
    @dannahari3918 3 ปีที่แล้ว +1

    Great video!
    I have questions regarding the second phase,
    The name "I/O Logic" is the parallel name for "Pending Callbacks" in NodeJs docs?
    if yes then:
    You said callback function of fs, network, encryption, etc. will run in this phase but according to NodeJs docs, they will run in the third phase.
    NodeJs docs:
    "pending callbacks: executes I/O callbacks deferred to the next loop iteration."
    "poll: retrieve new I/O events; execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()); node will block here when appropriate."
    So what is the right behavior? fs callbacks will run in the second phase or third phase?

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Hi Dan. Thanks for the feedback and for watching.
      Yes phase 2 and 3 are very misleading and the NodeJS docs don't really help much. From my experience phase 2 and 3 perform mostly the same operations, except that Phase 2 handles I/O related callbacks that were put off/postponed for the next event loop cycle before moving to Phase 3. I need to turn this into a discussion on its own, but truthfully I need to find better information on these 2 phases. Right now most articles written on these phases conflict badly with each other.

    • @dannahari3918
      @dannahari3918 3 ปีที่แล้ว

      @@BleedingCode Thank you for your comment,
      Like you I was confused by the articles.
      I have an assumption, in windows callbacks will run in phase 2 and in linux/unix they will run in phase 3

  • @neetneetov9835
    @neetneetov9835 ปีที่แล้ว +1

    10/10

  • @eeetube1234
    @eeetube1234 4 ปีที่แล้ว +2

    console.log() is not synchronous inside a worker thread. And messages sent to console from the worker thread can be lost if app exits.

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว +1

      This is valuable insight thanks for sharing.

  • @nicoflaig6749
    @nicoflaig6749 ปีที่แล้ว +1

    Regarding point 5. at the end of the video, to prioritize polling phase, is it best to just use setImmediate()? I have seen setTimeout(0) being used for that but doesn't that mean we might go straight back to executing the callback without getting to polling phase?

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว +1

      Yes setImmediate() is better if you want to execute the logic the moment the loop reaches the next iteration. Most people use setTimeout() either because they want to wait x milliseconds before the code executes, or because they don't realise that setImmediate() is an option.

    • @nicoflaig6749
      @nicoflaig6749 ปีที่แล้ว +1

      @@BleedingCode Got it, thanks for the answer.

  • @NoBody336Ilia
    @NoBody336Ilia 2 ปีที่แล้ว +1

    thanks a lot !!!!!!!!!!!!!!!!!!!!😊🙏🙏🙏🙏🙏❤️💕

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Always a pleasure 👍🙂

  • @bohdanlukashchuk9893
    @bohdanlukashchuk9893 4 ปีที่แล้ว +2

    Appreciate your video. Still have a few related questions:
    1. What about async ES imports? Are they handled as I/O and result is wrapped in Promise, so as microtask after that I/O or are they some kind more specific?
    2. And pretty similar, what about I/O that is also promisified? Will be there any difference in using a callback or promisified version? Or it will be just execution of Promise microtask after I/O phase and no difference in timings?

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว +1

      Great questions Bohdan. I need to check a few things regarding on point 1 and will respond asap 👍

    • @bohdanlukashchuk9893
      @bohdanlukashchuk9893 4 ปีที่แล้ว

      @@BleedingCode any updates on that?

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว +1

      @@bohdanlukashchuk9893 Hi Bohdan. Apologies for not replying sooner. Static ES Imports are synchronous and are loaded at the root of the app upon startup. Dynamic imports, as you have already stated, are wrapped in a promise and can be lazy loaded when required. What I haven't been able to confirm is if there's more async operations happening or if it's just a Promise Microtask. You can however answer that question using Async Hooks. Check my video on how to achieve this: - th-cam.com/video/yiBLmRRRx-k/w-d-xo.html

    • @bohdanlukashchuk9893
      @bohdanlukashchuk9893 3 ปีที่แล้ว +1

      ​@@BleedingCode thank you, was full of work, will check that video. You are doing great work!

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      @@bohdanlukashchuk9893 Thanks again Bohdan 🙏

  • @razx2009
    @razx2009 ปีที่แล้ว +1

    the above code where you tried calling settimeout and setimmediate inside the second settimeout, so my question is that did event loop check those two functions in second cycle?

    • @BleedingCode
      @BleedingCode  ปีที่แล้ว

      Correct. In the setTimeout phase.

  • @b7otato
    @b7otato 3 ปีที่แล้ว +1

    Why did you say console.log() is synchronous at 18:40? according to node.js documentation it's a wrapper for "process.stdout.write()" which is inherently non-blocking.
    To emphasize to importance, if you have a process it can exit before executing console.log(), which can be sometimes unintended, but if you want to make sure whatever you want to log is logged you would use console.error() or console.warn() for example, because they are blocking and the process will wait until they are executed before exiting.
    src: nodejs.org/en/knowledge/getting-started/the-process-module/
    also, I've noticed the Libuv mentioned around 7 phases, are they omitted in this video for a reason?
    src: docs.libuv.org/en/v1.x/design.html
    thanks for the effort

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      Hi Ariel. Thanks for posting. To answer your first question...console.log triggers a number of processes, 3 of which are asynchronous. However, depending on certain scenarios, sync tasks are also triggered, especially on posix operating systems - nodejs.org/api/process.html#process_a_note_on_process_i_o

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      To answer your 2nd question, I omitted the other phases because, as per my understanding, they are used internally by libuv and cannot be managed by one’s operations. I would love to know if I’m mistaken about this. Cheers.

  • @kanehooper00
    @kanehooper00 2 ปีที่แล้ว +1

    Hi Bleeding Code I've been watching all your Nodejs videos which are really good.
    In your diagram of the event loop you mention Promises are executed at each stage along with process.nextTick(). I don't see that written in the Node documentation at all.
    Also I have run some tests, and no matter how I set up my code, the promises are always executed after setTimeout and setImmediate, so I'm not sure that promises are executed in the way you say. If you have a source for this I am very happy to be corrected.

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Hi Kane. Thanks for your reply. Here's a link to the source code of the demo I used in the video - github.com/bleedingcode/nodejs-performance-optimizations/tree/master/01-testing-event-loop-phases

  • @inclinedscorpio
    @inclinedscorpio 2 ปีที่แล้ว +2

    If I am using await a lot in axios operation, then is it blocking the event loop? Is there any other way to speed it up given that I have to use all those calls in sync

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว +1

      Hi there. So, even though you're using await with Axios...the HTTP request itself is non-blocking, because it gets handed off to the OS. It's only when you're using await on userland code, that it will essentially block the event loop because it's a "Promise" microtask.

    • @inclinedscorpio
      @inclinedscorpio 2 ปีที่แล้ว +1

      @@BleedingCode ah thanks, btw what's userland code? Any example? Also tasks handled by OS are non blocking is it ?

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว +1

      @@inclinedscorpio Hi there. Userland code is your actual code/logic that you write in JavaScript. It's common term and you can easily google it for more info. Yes, tasks handled by the OS are asynchronous, or are managed by the libuv threadpool. Check out my Playlist on TH-cam as I discuss this in a different video :)

    • @inclinedscorpio
      @inclinedscorpio 2 ปีที่แล้ว +2

      @@BleedingCode Definitely will check.
      Thanks for making all these wonderful videos man. Respect++

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว +1

      @@inclinedscorpio Always a pleasure

  • @hamzafarhan348
    @hamzafarhan348 2 ปีที่แล้ว +1

    can you add breif description regarding the async require

    • @BleedingCode
      @BleedingCode  2 ปีที่แล้ว

      Hi Hamza. Sorry for the late reply. That's a good point. I need to maybe provide a video on how and where async require fits into the scheme of things. The truth is I don't know off hand and don't want to provide partially correct into.

  • @AdityaFingerstyle
    @AdityaFingerstyle 4 ปีที่แล้ว +1

    I am a little confused here.
    At 4:41 you said setTimeout & setInterval are handled in phase 1. But then later (at 20:30), you say - always prefer setTimeouts over setInterval because they are handled in phase 4.

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว

      Hmm apologies. Can you help pinpoint where In the video I contradict myself that so I can explain what I meant?

    • @AdityaFingerstyle
      @AdityaFingerstyle 4 ปีที่แล้ว

      @@BleedingCode Sorry. I have updated my comment and added another timestamp

    • @BleedingCode
      @BleedingCode  4 ปีที่แล้ว +6

      @AdityaFingerstyle Thanks for updating your comment. I’ve reviewed the video around 20:30 and I think I understand your confusion. 2 points regarding this:


      1. You mention that I’m telling one to always prefer setTimeout over setInterval. I checked the video and I don’t mention setInterval at that time? That been said, the reason I mention setTimeout and not setInterval is because setInterval serves a different purpose in that it’s a re-occurring async function. You would only ever use it for logic that needs to be executed continuously vs once off.
      2. Then, the confusion about Phase 4: I see where you got confused. I vocally state that setTimeout and setImmediate happen after the polling phase, but in the video I highlight Phase 4 whilst stating that, making it look as if both setTimeout and setImmediate happen in Phase 4. My apologies for that. What I meant is that setImmediate would occur after the polling phase because it’s in Phase 4, and that setTimeout will occur after the polling phase because it would only get executed in Phase 1 in the *next Cycle/Tick*.
      I really hope that makes more sense. Please let me know if I need to explain it further 👍🙂.

    • @AdityaFingerstyle
      @AdityaFingerstyle 4 ปีที่แล้ว +1

      @@BleedingCode
      1. I understand that they serve different purpose. I was more concerned about their precedence in the event loop.
      2. That clears it up. Thank you

  • @gouravkumar-fz3lr
    @gouravkumar-fz3lr 3 ปีที่แล้ว +1

    const to = 0;
    const main = () => {
    setTimeout(()=>{
    console.log("0 timeout");
    },to);
    setImmediate(()=>{
    console.log("immediate");
    });
    };
    everytime i ran. immediate came first. Can anyone give example where setTimeout comes first before setImmediate? looks like eventloop starts from polling phase

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Hi there. Sorry for the late reply. If you reference my video around 10 minutes in, you'll notice I explain a weird situation that occurs on load of the app. Is this not what's also happening to you? - 10:12

  • @royinchains
    @royinchains 3 ปีที่แล้ว +1

    Hello there!
    First i wanna thank you for this great video and explanations, very good job :)
    Now, i gotta say i'm a bit confused about what's going on during phase 2.
    What is the difference between I/O callbacks that get executed on phase 2 and those on phase 3? Both are I/O callbacks
    (the official docs explanation did not help me to really understand it).

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      Hi Roy. Thanks firstly for watching and for your feedback 🙏.
      Yes phase 2 and 3 are very misleading and the NodeJS docs don't really help. The truth is phase 2 and 3 are actually the same, except that Phase 2 handles I/O related callbacks that were put off/postponed for the next event loop cycle.

    • @royinchains
      @royinchains 3 ปีที่แล้ว +1

      @@BleedingCode And where do they get postponed? On phase 3?

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว +1

      @@royinchains Yes that's correct. What I can't guarantee is what method was used to defer the callback. I know it's not nextTick, setImmediate or setTimeout.

    • @royinchains
      @royinchains 3 ปีที่แล้ว +1

      @@BleedingCode Interesting.
      Thank you very much 🙏

  • @kuhaniresti
    @kuhaniresti ปีที่แล้ว +1

    is there any events that we can subscribe to the phases mentioned?

    • @BleedingCode
      @BleedingCode  6 หลายเดือนก่อน +1

      Sorry for the late reply. Off the bat I'm not sure, but I need to update these tutorials and will keep this in mind.

  • @gouravkumar-fz3lr
    @gouravkumar-fz3lr 3 ปีที่แล้ว +1

    In which phase eventEmitter works?

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Good day Gourav. I am nervous answering this question because I don't usually work with Event Emitters. However, as I understand it, natively it's synchronous logic. It's only when you apply asynchronous functions on the handlers that they become async, and in that case the phase it will be triggered is dependent on the async function you used.

  • @hazemabdelalim5189
    @hazemabdelalim5189 3 ปีที่แล้ว +1

    in which phase the thread pool will be executed ?

    • @BleedingCode
      @BleedingCode  3 ปีที่แล้ว

      Hi Hazem. If I understand your question correctly, transactions handed off to the OS can occur in any phase, but responses, will be managed in Phase 2. Whether these transactions get handed over to the Libuv Threadpool depends on if they're natively asynchronous or not. I recommend checking out my video - "INCREASE NODE JS PERFORMANCE WITH THREAD POOL MANAGEMENT" - th-cam.com/video/LC5FC3FdzAE/w-d-xo.html, where I explain the Libuv Threadpool in more detail.
      I hope this helps.