So that is the exact problem, is this very big event that is coming in. Now, to back my statement up, let's do a quick math of how all of this aligns in a central place, right? So if you see that spinner in our demo that we showed, right? If you want to get it done to be 60 frames per second, that means we would have a thousand milliseconds to render 60 frames, right? That's the math. And it means that you have 16 milliseconds for your JavaScript to run per frame.
Now, to talk more realistically, browsers usually take up four or six milliseconds out of this 16 millisecond time, which is their internal tasks, composition, painting, understanding how to parse HTML, JavaScript and stuff like that. So roughly if you talk about it, your JavaScript actually has only 10 to 12 milliseconds or less to run and render that frame in a constant time and avoid lag.
Now, let's try to see this small example of how your code actually goes through a browser's pipeline, what exactly is going to happen when your JavaScript is run, when your CSS is run, and when everything is compiled. So all of this, if you see it in one frame, all of this has to be done in 16 milliseconds or less amount of time. So your browser has to run JavaScript, it has to compute what are the styles, what is the CSS for it, it has to render all of the CSS, prepare the DOM, it has to compose everything and parse everything down and show the final result onto your webpage. Now as you keep on adding more JavaScript, be it like, we are not just using vanilla JavaScript right now. Maybe we are using CSS and JS engine like style components, we are using maybe read us or a library called React. Not to mention that React, adding React can add time to your JavaScript running. But the intention behind this example is the more time your JavaScript is going to take, the more time is going to get extended for your browser to catch up with this. Because all of this has to be done in 16 milliseconds. If your JavaScript is taking more time, you are going to surpass that 16 millisecond time limit. And because your subsequent frames are going to also have to catch up, your browser needs to skip some of the frames in between to catch up to the speed of getting this JavaScript done and also catering to the other frames that are coming in. And because of that, as I mentioned, it needs to skip the frames. And that is exactly what a jank is, because you just see a not moving spinner, because it had to skip all the frames to catch up to the speed, right. And that exactly was the problem there.
Now, coming back to the problem, which was there was a very big task that was blocking your event loop, right? Now, if I had to actually get this out of my event loop, right, and prevented from not blocking, my problem would get solved, right? So the other bigger tasks can keep running in some separate context, while my event loop is all free. So any bigger tasks are not blocking my webpages, UX or like it's not blocking or not jarring up the experience. And this is exactly the ideology of a web worker, right? So simple things, simple put, a web workers actually allow us to do work in parallel using separate threads, right? That is like the very simplest analogy. So you can do more amount of stuff in like some parallel context or like parallel threads to avoid your main thread to get blocked, right? And you can give all those bigger tasks to your worker threads.
Now, if I want to show you how this all works up, right? Let's understand the small analogy. There is your React app that is running in a totally different thread. And there is a worker thread that is again, a totally separate context. Now, what happens is we create a worker instance using the new workers, new worker API. And the worker that we get, we attach, we send a message to our worker thread, which is done by worker.postMessage API. So we send it a message that, hey, there is this one big task you need to do. And the worker receives it because we attach an event listener on the worker side, which is self.eventlistener. So make sure that workers do not have access to your window.
Comments