[♪ music ♪ I've been a dev for about 25 years. I'm the director of education for the SMYTH group, S-M-Y-T-H, and I've taught about 350,000 students web fundamentals in my courses on Udemy, Pluralsight, and other places, and you can find me currently on understandingreact.com for deep dives into React's source code.
And today's talk is entitled, If You Were a React Compiler. What is the most powerful tool in a developer's tool belt? Well, lots of people might have different answers to that, but I think the most powerful tool in a developer's tool belt is an accurate mental model, meaning that you have a reasonable understanding of how the tools you use work. Because if you have a reasonable understanding of how the tools you use work, you make good architectural decisions. You have confidence as you use the tool. And you're able to debug when things don't go as expected.
So in this talk, the goal is to build an accurate mental model of how the new React compiler works. And in order to do that, we're going to pretend that we're the React compiler. We're going to take an example piece of code that's slow, and we're going to try to do what the compiler does. So if we're going to be the compiler, we have to have some ground rules. What's our focus, and what's our goals? Well, we know that React by design calls the functions we give it, that is to say our components, over and over again. Now, React likes to call that components re-rendering. But really, we give React our function objects, and then React chooses when to execute and re-execute those functions. And that means that React's performance can be tied to how often our functions are called, and how much work our functions do.
So what's our goals? Well, our goals as a compiler are to minimize re-renders, that is minimize how often, how many times, our functions that we give React are called over and over again, and to skip expensive calculations if we don't need to run them. And that will improve our performance. Now, there's been some controversy online about whether React compiler is a compiler, so let's get that out of the way. In computer science curriculums, often folks are introduced to compilers when talking about converting higher-level programming languages into machine language, and that's true. But it's also always been true that there's such a thing called a source-to-source compiler. Sometimes we call that a transpiler. And that's essentially what React compiler is. Source-to-source, it takes code as an input and outputs equivalent code, code that does the same thing that our original code does, but with some extra goodies. And that's what React compiler does. So if we're the compiler, we're going to do the same thing that the React compiler does.
Now, when a program running as a compiler runs, it analyzes the text of the code. So that's what we would do. Now, in the case of an actual compiler, it might convert the text into something called an abstract syntax tree. So it might take a line of code that we write and then convert it into some kind of object structure, which can then be looped over, reasoned about, et cetera, and then output new text, new code. Source-to-source.
Comments