In this talk, I'll be talking about concurrent mode, more on my mental model toward concurrent features. So, there is no concurrent mode as such coming in the React 18, it's more of like an incremental concurrent feature.
About me, I'm Sudhanshu Yadav, I work at Proficy as a front end architect, I've authored Brahmos, the React library, and also have open sourced a lot many other tools. I am an internal fanatic, and I love discussing the internals of the library I use, I like making theories around it, you can find me on Twitter or you can check my project on GitHub.
Before we start, let's understand why we need concurrent mode, what it means for a React application or any application in general. The most important reason why concurrent feature is there is to provide a better personal experience. Now, lot of libraries focus on improving the performance of the library itself, which should be the priority, but sometimes those improvements are not noticeable by user, and if a user doesn't feel your application to be smooth, then your application is not smooth. They should pursue your application to be smooth.
There is a problem with the current patterns, which comes around improving personal experience. They have a good context on the application, but they don't have control over your rendering phase. Because of which those patterns can't effectively use resources. With concurrent features, it allows to effectively use all the resources available while keeping the application responsive. Concurrent features also enable background rendering and that kind of enables a lot of things like suspending a render, pausing, resuming it back, or like batching renders together. A lot many things. And it also removes a hard part from a user length, is about orchestrating the whole rendering thing. It kind of like provides declarative APIs which can help you to build, more define the orders, give a hint to the React or application like a library that this is the way I want my rending sequence to be, or like if I want the best things to get a pre-render something or lazy load something. All of those things can be done in a more declarative way.
Now definitely concurrent feature has a lot of scope, but what enables the concurrent features? And the first and most important pattern, I would say, is the time slicing. To understand time slicing, let's see a react tree here all the nodes, you can think of a component which takes a little bit time to process and how update would happen like you change a state in any component and then it will trigger a re-render on that component which internally would trigger re-render of their children and all of these things will happen asynchronously at one go. And once you find what all changes are required for the actual DOM, after knowing those changes, you commit those changes to the actual DOM.
Now, let's see how it plays on a frame timeline. Most of the libraries try to achieve 60 FPS and that usually means you have a frame which is of 16 millisecond and you get 16 milliseconds to execute, not even actually 16 milliseconds, less than that, to execute, to do all the JavaScript stuff. So, if you have, there are two types of tasks, JavaScript task and browser task. JavaScript task would be like the processing of the component, and browser task would be like painting on the browser or giving a feedback of user input, animations or anything. Now, if your JavaScript is big and it spans across multiple frames, then what will happen if there is a browser task in between. It has to wait until all the JavaScript tasks is finished. So basically, your application will sutter a little bit because there will be frame drops. So with time slicing, the idea is that you have a big work, so break that big work into unit of works. And after, like process unit by unit and after every unit, check whether browser has something to do and basically give a breathing space for browser. With that let's say if a browser task come in between, it can easily fit in your render side like it can easily fit between your Javascript task.
Comments