This could be a count, or this can be a node in our case. We don't necessarily know the values beforehand, and so we put a placeholder node inside of these. So essentially, we have this tree, and the dynamic values that are put in the nodes are marked as potentially could be a value there.
And so what we do now is just traverse the tree like is usual. We check the first one as a placeholder, so on and so forth, and when we hit a placeholder, we can create something called an edit map. This is the secret sauce of the block virtual DOM. Essentially, what we say is when node 1 changes or this data changes, we change this node. Essentially, we have this relational mapping between the data and the UI. And we can do this for every single placeholder node on the tree.
And during runtime, the edit map really, really shines. You can see here when we're trying to update node 1 and node 2's value to 3 and 4, respectively, you can see that we only have to do two diffs. So we check if the data is the same, 1, 3, yes, it has changed. Check it again, it has changed. And we can make updates to the virtual DOM. You notice here that the virtual DOM would take five diffs, but with the block virtual DOM, it only takes two. And this scales infinitely. This is essentially an O of 1 optimization to the virtual DOM. And this is really cool, because we have the same example of 200 nodes on the page. Now you can see that the change is O of 1. Every single time you update, there's an O of 1 change. And it's really, really fast.
So I work on a project called MillionJS. It implements the block virtual DOM as a drop-in replacement for React. Using that, it is 30% faster than Preact, which is a lightweight React alternative, and over 70% faster than React on synthetic benchmarks. And so this is really, really indicative of better performance using the block virtual DOM, particularly on benchmarks compared to traditional virtual DOM alternatives. So essentially, MillionJS and the block V DOM are faster than React. And if you don't believe me, you can take this example. I have a really bad computer, so bear with. When I click on this button, it's really, really red, and that's indicative of bad performance. But when I switch to Million, when it loads, if it doesn't beach ball me, you can see that when I click here, it's way better.
Comments