Rich Harris - Rethinking reactivity
Video Statistics and Information
Channel: You Gotta Love Frontend
Views: 214,741
Rating: 4.9795675 out of 5
Keywords: YGLF, Code Camp
Id: AdNJ3fydeao
Channel Id: undefined
Length: 36min 45sec (2205 seconds)
Published: Mon Apr 22 2019
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
There's a fair bit of nuance that this is glossing over, IMO. An approach that avoids the temporary object creation overhead is definitely a good idea for tight update code paths. That's a use case Svelte is ahead in.
For the kind of React apps we create though, "replacing a screen" (or some part of it) is a much more common (and perf sensitive) transition than a raw update to an existing hierarchy. In that case highly optimized targeted updates don't give you that much. Sure, it's nice to avoid extra GC overhead from short lived objects even for the mounting case, but GCs have also been getting much better over the years.
We've found that when there's a very tight update path, you can optimize it by going a level down to imperative DOM mutation for that particular component. E.g. if you have a table with a thousand rows that update every frame. You can usually just do that part with refs and raw DOM access. So we haven't historically invested as much into this use case (while Rich has β because he works on interactive dataviz which primarily does those kinds of updates).
There were, however, some problems in React that we did find very challenging:
Even if we can optimize individual components, it's hard to optimize arbitrary JS that just needs to run as part of rendering those components. View library can't solve this for you β some computations just need to happen. Especially on first mount! (Which, as I noted, is the majority of interactions that are perf-sensitive to us.) So how can we keep the browser responsive in this case? It's hard if all your work is synchronous and non-interruptible.
React paradigm makes view a function of props/state. But this means that transitions are usually flushed to the screen too early (as soon as you navigate, basically). But this is undesirable because it creates too many intermediate loading states shown to the user while you're fetching data, components, images, etc. So how do we avoid too much jank from being "faster than desirable" for a good perceived performance?
We're working on Concurrent Mode which in our view helps solve both of our problems. I elaborated on it here: https://mobile.twitter.com/dan_abramov/status/1120971795425832961. Concurrent Mode is not a panacea and comes with its own tradeoffs. (E.g. you need other code on that page to cooperate and not block the main thread for long periods with stray timeouts or intervals.)
I do think Svelte has great ideas though, and we'll be looking at adopting some of them where it makes sense in the future. E.g. it's nice to be able to lift state higher up without worrying about memo'ing unrelated things while you do that. This is something a compilation step could definitely help with in terms of ergonomics.
the vdom isn't the inefficient bit, its the size of the runtime that we react users ship that might not be used. lets be clear about what is being discussed or this will spiral out to irrelevance very quickly
edit: see danβs response below https://reddit.com/r/reactjs/comments/cgqzjr/_/eun1lu3/?context=1
Svelte3 is showing us the approach for the 'frameworks' in the future. Just a compiler - not a framework, simple and faster.
Evan You (Vue creator)'s talk on front-end libraries is quite related to this topic of virtual dom vs raw update: https://www.youtube.com/watch?v=ANtSWq-zI0s
He gives a mostly unbiased view as to the main tradeoffs.
Finally someone put transitions and animations built into framework. These have been so hard since component based frameworks took off.
You can do same think as Svelte did in React. Some recent POC https://github.com/sokra/rawact
React isnβt inefficient though?
JqueryAngular ReactSvelte!Great initiative into getting rid of all that overhead and getting UX to work on simpler devices.
This is gold.