Hey, everybody. How are you? My name is Evitar Alus. I'm a frontend engineer at Meda in Tel Aviv and I'm the author of several open source packages, one of which is this cool little package called emoji-pkreact that allows you to put an emoji-pkreact right inside your React application on the web with ease.
And today I want to share with you a little trick that I came up with to improve search performance in React applications and React packages. And when I started building emoji-pkreact back in 2017-ish, I thought it would be pretty straightforward to build an emoji-pkreact component with all the features of an expected emoji-pkreact that would also be performant and easy to use. And quite immediately I started receiving issues about performance, for example, that search is extremely laggy and, obviously, my response was that it does not reproduce. Obviously, that was wrong.
Search was actually laggy. And the reason for it is quite understandable. You see, we have roughly 1800 different emojis being rendered on the Emoji Picker. And when you interact with each of them, you have some state updates. So, for example, when you hover or when you type something in the search, you make changes to the DOM and to the elements. And when you deal with so many elements, and when they have so many different states that they can be in, it can be quite intense to work with. And especially here, we see the search bar is up here and the emojis are there. Which means that they're very distant in the React tree. Which means that we have to have some almost global Emoji Picker React state to manage the filtering capabilities. And then when we type some character inside of the Emoji Picker, we go update the state, run some search algorithm that I try to make performant, and then React goes and does DOM diffing and then only applies the changes to the DOM, and each of these steps take time. And when we deal with 1800 emojis, that takes a lot of time.
Now, if you look now, it looks pretty immediate. It works pretty fast, but I want to show you just for a second what it felt like before Emoji Picker React had these capabilities and the improved search. So I'll type something and you'll notice slight lag, even on my MacBook Pro. You felt that? There was some lag between my character being typed and the changes appearing on a screen, and that's a high-end MacBook Pro. Let's try to do some slowdowns, so 6x slowdown to simulate an older machine, or a slower machine, and let's see what it actually feels like for a user in the wild. And you saw that, right? That's not ideal. We gotta fix this. Just to show you what actually is going on when we type each character, when we type each character, we actually go out and re-render everything. And the reason is, again, that we have that top level tree as state and that we actually have to modify all the elements on the DOM. But do we, really? And this is what I wanted to find out. Do I really have to manipulate the DOM and change all the emojis just to make them disappear? Or maybe I can somehow skip it? So I went to look at the actual emoji elements that I created and what I have here, each emoji is a button component or a button element and they have an image inside them. They sometimes have a span with an emoji character.
Comments