Functions are an integral part of all JavaScript applications, React apps included. While I've written about how peculiar their usage can be, thank...
For further actions, you may consider blocking this person and/or reporting abuse
Sorry to disagree but it is not functions that are slowing down your application.
It's REACT. And in particular re-rendering.
To blame functions (or the poor use of them in React) is pretty ridiculous, but it fits with my claim that most "front-end devs" are really "React devs" and see all of HTML, CSS, and JS through the prism of the React library/framework. How sad. They are so blinded by their abstraction layer that they can no longer see the forest for the trees.
Dump the React and the problem goes away. The DOM is actually quite fast. React is much slower.
So what you're saying is that code without React, like this:
Should be faster than this React code?
Hmm. There are these languages called HTML and CSS? We can actually write them separately, rather than creating our entire page with JavaScript. But that's beside the point.
I am not going to waste time writing a lot of code to debunk this. I will simply make this point:
In the first instance, you are using JavaScript to create HTML on the fly.
In the second instance, you are using JAVASCRIPT to create HTML on the fly.
The only difference is that in the second instance, you've hidden some of the JavaScript behind the React library.
As React is just JavaScript (so far), and it works by generating HTML (and sometimes CSS) in the browser using JavaScript, then there is nothing you can do faster with React because I can simply write my JS to use the same tricks that React is using.
My guess here – and again, I'm not going to waste time on digging into it, but be my guest – is that React is creating the update in full as a string and then using
innerHTML
to update the DOM all at once, resulting in a single re-render (or something like that). But you've written your "JavaScript" cleverly to update the DOM over and over again. You're comparing apples and oranges (I didn't read your code closely, so correct me if I'm wrong). Kind of disingenuous. I'm guessing that you knew this in advance.The React code is also more complex, longer, and harder to understand. But even if we say that React is faster than writing JS by hand, well, it is not as fast as SolidJS by a long shot. Or Svelte. So I still don't see the "benefit" of React. The only thing it has going for it is a huge "ecosystem" and a lot of cheerleaders. But that just encourages devs to use ever more dependencies on other people's code with all the security risk and bugginess that brings.
I'll stand by my initial comment. Where are your benchmarks by the way?
Right here :)
This is the JS-only code:
This is the React code:
The code is about as apples-to-apples as they get. The only difference is that React's reconciler has saved us from re-rendering un-needed elements.
To do the same in JavaScript you'd have to write your own variant of a reconciler or change the way renders are made to be more complex from a DX perspective.
You're welcome to continue thinking that React is a bad tool, and there are valid criticisms of it.
The point I was making wasn't "React good" or even "No-React JS bad" it was "All languages/codebases have weaknesses; even without frameworks."
The fact you immediately took "This is how to improve your React's codebase's performance" to mean "Web developers are bad at their jobs" says more about you and your lack of ability to empathize with other developers (either that, or a lack of understanding of conceptual naunce) than it does React, React developers, or the web ecosystem as a whole.
Good day.
Oh, I'm so sorry. I thought that the article had the title, "Functions Are Killing Your React App's Performance", which clearly states that functions = bad. I must have missed where it was entitled, "This is how to improve your React's codebase's performance". Did other readers see that title?
Most "readers" don't read articles. They read headlines. At best they scan articles. So what you say in your headline matters quite a bit. The original article has a clickbait headline. I reacted to that. I did not bother the read the article; neither do I intend to. It was a fail before the first paragraph.
So why didn't you just give it the right title in the first place? Hmm. And I don't remember saying anything about web developers being bad at their jobs, although now that you mention it, after decades in the business I have to say that most are indeed pretty mediocre. And lazy. Many, at least in enterprise, hardly do any work at all and most of that is merely configuration.
I was talking about devs in general. If you make it personal, well, that's your choice.
Why doesn't React modularize their code so that if all I want is a reconciler, then I can import only that? And if React is using a reconciler and the plain JS code is not, then how is that "apples to apples"? Let's have a 100km race. Only you are on a bicycle and I get a car. Fair, right? You'd bet big on yourself, no?
What is astonishing to me is how overly sensitive so many authors are. My original response was short and to the point. It was about the problem being in the React code, which you admit: "This is how to improve your React's codebase's performance".
I went on to claim that most "React devs" have become alienated from the HTML, CSS, and JS that actually runs the Web. I think there's plenty of evidence for this, but readers can make up their own minds.
But more importantly: I am no one important. You could have ignored me easily. Instead you've gone to a lot of trouble to try to crush me. Wow! One nobody says React is over and it's time for a pile on? Why so insecure? If React is so great, doesn't it speak for itself?
If I were a troll, I'd be high-fiving all my fellow trolls right now. It's almost a shame that I'm serious.
Oh, and news flash: everything that everyone says says more about themselves than anything else. Consider that when you write.
Heh. I did go back out of curiosity (maybe I can learn something about vanilla JS!) to look at your "vanilla" example. But it made no sense to me. It seems to be doing a lot of unnecessary stuff. I thought we wanted to click a button and add numbered items to a list. It looks to me like you wrote the React version first, and then copied it into plain JS.
Isn't this much simpler?
If you really want the layout shift, change line 17 to
listRoot.prepend(item)
.That's the entire point. My point was that the vanilla JS was not optimized. I intentionally made it worse than it could have been.
Once again; I absolutely understand hand-written JS can be faster than any framework. My point is "Any code can be written inefficiently".
I understand you're sold on the idea of telling newcomers to focus on the fundamentals. A noble effort.
What isn't noble is telling them that they're mediocre, should stop learning the tools they're already using, and that they "can't see the forest for the trees".
I get it, you don't like clickbait. Good on ya. But it gets people to read, is clearly tagged with
#react
and#reactnative
and generally is an informational guide.I'd implore you to, rather than fighting in the comments about how someone's work could be improved because you read the title and nothing else of the article; go write more of your own content like I see you've done prior.
Not optimized? I wrote my version by starting from scratch with simple acceptance criteria and then not writing any code that wasn't necessary. No optimization required. With something more complex, I might have to revisit it once or twice. Seems like you're the one expecting people to write mediocre code.
Your article is enormously long. If you're going to lecture me on how important clickbait titles are to getting readers, you might want to do some research on article length. You could have made the same point much more succinctly and gotten more readers.
But I actually like your article the way it is. You get into reconciliation and React Fiber and trees and all sorts of deep computer science-y stuff (losing most of your readers, BTW). Why do you need all that React power?
I have a site with one page that has maybe 1000+ DOM objects on it, and Lighthouse complains that it is too big. How big are most pages? Why do they need all this complexity? What is it that we're trying to do that requires layer after layer of abstraction? People spend years learning not how to build web applications, but how to use React. That's crazy.
And that was my point.
As for writing, I do plenty – in the comments. Why on Earth would I write articles and have to compete with clickbait titles like yours when there is a ready-made audience pre-selected by you? It is much more efficient to piggyback on your work, and I don't see how it diminishes your work at all. After all, your article stands or falls on its own merits. If anything, I am driving more traffic to your pages. You should be thanking me, not imploring me to go away.
I appreciate your feedback about the article.
I know it could have been shorter and I likely could have turned it into three distinct articles. Was considering doing so, but felt they cross-references themselves too often to make sense.
Plus, I have real world data from my time writing professional dev content that shows that despite higher bounce rate longer content has more average time spent on page and higher site/cross-content retention.
Great article, every React dev should read it! 🔥
Thanks so much for the kind words!
I watched this beautiful React Documentary recommended here, which is about the history of React. As far as I understood, React was just created to let people NOT think about implementation details. Your post sounds a bit like React lost it´s way...
Letting people not focus about implementation detail is a nice ideal; but IMO practically impossible, regardless of which framework is where.
Even prior to Fiber, these issues were always present in React.
Further, even within frameworks that have nicer DX around performance, say, SolidJS with signals, there's still going to be some related issues that are caused by not using memos or whatnot.
This is all to say that as long as engineering exists, there will be nuance and context that's required to keep in mind to maintain performance. There will never be a one-size-fits-all performance improvement; it's tradeoffs all the way down.
LOL.
So whether I'm right or wrong is inconsequential? Alert the media! Performance no longer matters.
It's astonishing to me that there is still anyone in tech who thinks that popularity = performance. React is popular because it's popular. Devs and companies choose React because other devs and companies are using React. It has a big ecosystem because it has been around for a long while.
But for that same reason it is legacy code, filled with bad ideas that just won't go away. It's not that the folks behind Svelte or Solid or any of the other options (hey, there is even plain JavaScript and the DOM) are smarter, they just came along later and learned from React's mistakes.
Now we can sail along on that React inertia, unwilling or afraid to try something new, or we can make efforts to improve by considering and even adopting new approaches. Even React does this, switching to hooks and now talking about signals.
But most importantly, writers on Dev.to should be ready to have their ideas and claims challenged. Is this about writing better code and helping newbies to get on the right path, or is it about stroking the egos of those who write here? I know, dumb question. Of course it's about ego.
I think the crappy attitude is yours. I made a comment about React and gave an explanation for why so many devs cling to it, namely, that it's all they really know these days. It was a call to get back to basics. You decided to take it personally, but I don't even know you. So that's on you.
This article is another perfect example why react a poorly engineered tool. All this useMemo and other internals should have never been exposed to developers.
Perfect solution for it - use solid.js and enjoy hassle free development.
Even Solid has an equivalency to
useMemo
: solidjs.com/docs/latest#createcomp...There is no silver bullet to development.
Yes, agree, no silver bullet. But if you dare to compare, react is a shit bullet and solid is wooden one :)
Now seriously, after 4 years of vue.js switching to working with react, makes me cry every day. It is just a bad piece of software. Will do my best to transition my company to Vue or at least solid.js.
The SolidJS community doesn't promote this panacea-like mentality. We're trying to build an engineering-first community, not a hype based one. I hope our community members/fans/supporters keep this in mind in the future when engaging with other communities. Let's be respectful to other approaches and ideas.
Class act reply. Sincerely, thank you David 🙏
A great article..
Did you even read what I wrote? I'll let any other readers who make it this far decide for themselves. But then they were going to do that anyway.
Here's the best part about all this; React isn't even my favorite among React, Angular, Vue, and Solid. In fact, it's by a wide margin my least favorite of these tools.
This is where you're mistaken - I have no particular marriage towards the tool but acknowledge its utility. One such example:
Web-tooled-based native apps? Hard to beat React Native. NativeScript is cool and all but lacks a similar-sized ecosystem, Something fairly key for many businesses.
Code-sharing with web and mobile using React Native, in particular, has been a huge boon for my team.
This impacts our apps; of course, it does. The bundle of our web applications is larger than I'd like them to be. But does that mean I throw the baby out with the bath water? No. We're a very small team in charge of a wide set of applications, and we're only able to manage as much work as we do, thanks to this code-sharing quality of React.
Performance for us isn't first-and-foremost but isn't unimportant either. It's a matter of balancing the business' needs and the user's needs.
Would it be possible to rewrite this into a smaller bundle with dedicated native apps, Solid.js, or some other set of tooling? Maybe, maybe.
But would it be feasible or make sense to the business to do so? In our case, absolutely not.
I'll reiterate my stance from another comment once again; React is not some greater good that will save your applications. It's even up for debate if React has earned the popularity it's garnered.
But for as long as it's used, there's no reason I shouldn't and can't talk about how to improve your existing React apps.
And that's the key: you're complaining that you're not able to criticize me and that my writing is only to "stroke the egos of those who write here". And yet, it's not me complaining about how other devs aren't using tools I approve of; it's you.
Newcomers are going to learn React whether or not you like it. While this is the case, we must teach them the concepts of the frameworks they're using. Not only does this improve their current applications, but it forces them to restructure their approach to development in general. The more devs we can get to think about how a reconciler works, the more devs will be likely to - as you put it - "see the forest for the trees".
I encourage and welcome you to criticize my writing - it's the only way I improve, and goodness knows I need it. But let's do that, then, shall we? Criticize my writing, not divert attention away from the topic at hand so that we can simply complain about other's tech stack when I'm actively trying to improve the experience for those using it through education.
Sorry, but you're confused. I'm not responding to you. I honestly don't care at all what you think. We'll never meet. Nothing you do in your career will affect me in any way. Frankly, I don't care if you want to use scriptaculo.us or do everything in Cold Fusion. Have at it.
I am, however, a bit unhappy that you write clickbait titles (I note you've switched to defending yourself and don't mention the title at all), but only because they mislead newbies. Be honest about what you're saying. Maybe even start with "IF you are using React, here's something to look out for." At least that doesn't sound like React is the Web, which seems to be what a lot of React devs believe.
But as I say, I am not responding to you. My comments are actually directed at readers (more likely scanners) of your article, and my goal is precisely to "divert attention away from the topic at hand" so that readers consider that there are other ways to solve this problem (including giving up on React completely). And to correct anyone who, not reading the article itself, assumes that you're going back to recommending classes.
In short, I am addressing the underlying assumptions of your post – the part that readers might take for granted.
So it is the bystanders for whom I write, and my intent is to introduce other ideas. I am under no illusion that the great mass of readers will simply follow the herd. But maybe I can pick off a straggler or two.
I repeat what I said:
Your reaction is essentially to set up straw men to prove I'm wrong (about which of the above three, exactly?), and the other commenter here is only concerned with taking all this personally, like I have any clue who they are or care. And then both of you keep doubling down. You just can't make this shit up.