I've been using React to make a 60fps music game, and this article covers basically all of the issues I ran into:<p>- Originally, the game was rendered with SVG elements, so it was very important to use the shouldComponentUpdate hook to ensure elements weren't rendering unnecessarily (and using CSS transformations to position them where possible, as that's way faster than updating SVG coordinates!)<p>- I had something similar to the function.bind problem since some of my components had a map of hotkey handlers that I was (naively) returning from a `getHotKeyHandlers` function, meaning the handlers were redefined every render.<p>- I wasn't caching/memoizing calculations done to the Redux state as well as I should have (Reselect is very useful for this!)<p>I ended up deciding SVG wasn't fast enough for my use case and rewriting the core game loop rendering to use Canvas, which was so much faster that I could be a lot lazier about optimizing things. Everything around the canvas is still rendered through React components (i.e. in game UI and menu screens), and the state is still handled through Redux. It's a very fun way to write a game :)
Thanks for the valuable tips!<p>Currently, my biggest 'bottleneck' when developing React applications is managing complex forms. Right now I'm using redux-form [0] which is a great tool, but not very performant on dynamic deep/complex forms (more info and partial hacks here: [1]).<p>Other than that, I don't have performance problems, but I'm curious when will I hit Redux limitations (single store sounds great from the conceptual point of view, but I'm not sure about the performance one). Also, can second ImmutableJS recommendation, after a while of using it's completely natural to use.<p>[0] <a href="https://github.com/erikras/redux-form/" rel="nofollow">https://github.com/erikras/redux-form/</a><p>[1] <a href="https://github.com/erikras/redux-form/issues/529" rel="nofollow">https://github.com/erikras/redux-form/issues/529</a>
The biggest performance issue I've encountered is with long lists. The most basic implementation means you will have to instantiate a new component for every item in the list even if only one of them has changed. I haven't spent that much time looking into it, so the bottleneck might be function binding (I hadn't thought of that before) but if anybody has an approach to this any ideas are appreciated. Maybe only pass an id as a prop and grab the rest from context?
I want to see an article which shows how to code a fast rich text editor in React.<p>I tried to implement one by representing the text as a long list of character components, thinking react could handle this by only executing small incremental updates, but the whole thing became slow anyway.<p>Am I missing something, or is react not up to this task?
I don't see enough mention of this, but if you want immutability (to make shouldComponentUpdate easy) icepick is a better choice than Immutable.js for a great many applications:<p><a href="https://github.com/aearly/icepick" rel="nofollow">https://github.com/aearly/icepick</a><p>It's tiny but offers a number of utilities that are more convenient than what you get with ES6 spread syntax. The tradeoff with Immutable.js is that if you have huge single objects (arrays with many elements, objects with many keys), then making copies will get more expensive than with Immutable. Otherwise, icepick is all win.
These will _probably_ be useful but I did not understand any of the terms mentioned in the article. I guess I haven't hit any major bottlenecks yet. Bookmarked!
I just went through a rather large profiling/optimization run on the my site, BitMEX (we're a Bitcoin derivatives exchange). Performance is paramount in trading, so our site has to be fast. Here's some additional tips:<p>0. shouldComponentUpdate (SCU) is everything. Consider creating a base React.Element extension class. I define a base shouldComponentUpdate and some useful utilities on it. Having it in one place is really useful because you can log its output from every component and watch how your whole application updates. It makes it easy to see useless rerenders.<p>- One very helpful addition in our app has been a static 'SCUSelector' property, which tells our shallow shouldComponentUpdate to ignore certain props/state/context. This helps in combination with rich componentWillMount() or getInitialState() functions to precalculate expensive data that updates rarely/never.<p>1. It seems the `connect(component, selector)` model [0] encouraged by Redux to hook up application data to deep(er) components is much better for performance. You want to avoid renders in as many tree branches as possible. To this day, the root element (`<App>`) still holds most of our data and fans it out to the Router, Header, Sidebar, etc. This root-level render must be <i>extremely</i> fast because it runs on every tick. Hoisting any and all data that can be made constant is a huge help.<p>2. We saw massive (50%+) performance gains from Babel's constant-elements and inline-elements optimizations [1]. Careful with it - there's is a nasty bug [2] that <i>only manifests in production</i> you need to be aware of if you're deploying this.<p>3. Immutable-JS is kind of a wash. I'm not a big fan of the API or how easy it is to nest mutable objects and arrays inside immutables. The new array/object spread syntax in ES6 makes it easier to construct unique objects every time data updates, so you can do a shallow shouldComponentUpdate.<p>4. Watch out for function identity because it will blow SCU [3]. Either ignore the property explicitly, or use a binding helper like the one I posted on that issue [4].<p>5. If you're doing expensive data computation, such as update/insert/delete/image on websockets, profile it. If you're accidentally causing deopt, you'll feel it. Make sure the libraries you use don't cause deopt either. A few simple tweaks [5] can give you 2-10x performance boosts on hot functions [6].<p>6. If you haven't already, watch videos on how V8 optimizes. It will help you write fast code in the future.<p>7. Keep watching your app! The new React devtools can highlight components as they update. This is really useful to see what work is being done and how you can improve.<p>---<p>Links:<p>0. <a href="https://github.com/rackt/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options" rel="nofollow">https://github.com/rackt/react-redux/blob/master/docs/api.md...</a><p>1. <a href="https://babeljs.io/docs/plugins/transform-react-inline-elements/" rel="nofollow">https://babeljs.io/docs/plugins/transform-react-inline-eleme...</a><p>2. <a href="https://github.com/facebook/react/issues/5138" rel="nofollow">https://github.com/facebook/react/issues/5138</a><p>3. <a href="https://github.com/facebook/react/issues/5197" rel="nofollow">https://github.com/facebook/react/issues/5197</a><p>4. <a href="https://github.com/facebook/react/issues/5197#issuecomment-177991647" rel="nofollow">https://github.com/facebook/react/issues/5197#issuecomment-1...</a><p>5. <a href="https://github.com/petkaantonov/bluebird/wiki/Optimization-killers" rel="nofollow">https://github.com/petkaantonov/bluebird/wiki/Optimization-k...</a><p>6. <a href="https://github.com/AmpersandJS/ampersand-state/pulls?utf8=%E2%9C%93&q=is%3Apr+strml" rel="nofollow">https://github.com/AmpersandJS/ampersand-state/pulls?utf8=%E...</a>
> This post is for those of you with a complex React application. If you're building something smaller, you might not need to focus on performance yet. Don't prematurely optimize! Go build things!<p>This is bit meta, but it's really, really awesome to see an article on performance begin with this. Well done author :-)<p>It instantly inspires confidence that the material to follow is worthwhile reading. Knowing the author has started out with this philosophy I trust more that what's covered will focus on things that actually matter, rather than just being a crawl through every and any technique or quirk that they've come across, regardless of its relative impact.<p>Performance is one of those things where more is obviously better, all other things equal, but that last 20% of effort invested is almost always time that could be better spent improving other aspects of the average <i>use case</i> of any given product, let alone the average product.
TLDR for ClojureScript users: most techniques described here do not apply to you. Whatever React library you use (Rum, Om, Reagent, or other), most work has already been done for you through immutable data structures. Unless you go out of your way to create lots of components that react to something different than their parameters, shouldComponentUpdate will be handled quietly behind your back and you'll get impressive performance right out of the box. And no need to even learn about the differences between deep/shallow comparisons, either.