11/2/2022 0 Comments Nodejs event loop![]() ![]() SetTimeout(count) // schedule the new call (**) For server-side JS that’s clearly noticeable, and if you are running it in-browser, then try to click other buttons on the page – you’ll see that no other events get handled until the counting finishes.Īlert("Done in " + (Date.now() - start) + 'ms') #Nodejs event loop codeIf you run the code below, the engine will “hang” for some time. ![]() ![]() To demonstrate this approach, for the sake of simplicity, instead of text-highlighting, let’s take a function that counts from 1 to 1000000000. Highlight first 100 lines, then schedule setTimeout (with zero-delay) for the next 100 lines, and so on. We can avoid problems by splitting the big task into pieces. It may even cause the browser to “hiccup” or even “hang” for a bit, which is unacceptable. While the engine is busy with syntax highlighting, it can’t do other DOM-related stuff, process user events, etc. To highlight the code, it performs the analysis, creates many colored elements, adds them to the document – for a large amount of text that takes a lot of time. Use-case 1: splitting CPU-hungry tasksįor example, syntax-highlighting (used to colorize code examples on this page) is quite CPU-heavy. Now let’s see how we can apply that knowledge. That happens when there are a lot of complex calculations or a programming error leading to an infinite loop. So after a time, it raises an alert like “Page Unresponsive”, suggesting killing the task with the whole page.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |