Blocking Code Demo 💻
- Cluster is covered later in this course, but you can skip to it now: https://node.university/courses/you-dont-know-node/lectures/1590294
So, here's the demo of some blocking code. So, its very fun, because you don't see that for-loop like this with a lot of iteration. We don't see that code so often in the real-life situations.
Usually the blocking code is buried, sometimes deep. So, that's why I think it's fun for the purpose of this course and this exercise to just play around and see how it's actually working. So, I'm going into the code folder and then I'm executing node blocking.js.
And my guess what will happen, we'll capture the date, so start, will be our... Date.now would be the time stamp, then we would capture the end time stamp and we will log the difference on line number eight. And let's see what results I have. So, 546 milliseconds the first time. The second time, a little bit longer, 596 milliseconds and the third time is even longer, 608 milliseconds.
So, during that time, this particular process cannot do anything and that's why it's a bad idea to use the blocking code. And if you really, really, really have to use blocking code, then my advice is to use clusters. Because with clusters, you can have multiple processes and then even if one process is busy doing this type of blocking work, the other processes will still kind of catch-up and handle the rest of the job.