Batching
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
Batching
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
Sub-tree Rendering
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
Sub-tree Rendering
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
With shouldComponentUpdate, you have control over
whether a component should be re-rendered.
Sub-tree Rendering
• Whenever you call setState on a component,
React will mark it as dirty. At the end of the event
loop, React looks at all the dirty components and
re-renders them.
• This batching means that during an event loop,
there is exactly one time when the DOM is being
updated. This property is key to building a
performant app and yet is extremely difficult to
obtain using commonly written JavaScript. In a
React application, you get it by default.
shouldComponentUpdate
This function is going to be called all the time, so
you want to make sure that it takes less time to
compute the heuristic than the time it would have
taken to render the component, even if re-rendering
was not strictly needed.
2 Basic Assumptions
• Two components of the same class will generate
similar trees and two components of different
classes will generate different trees.
2 Basic Assumptions
• Two components of the same class will generate
similar trees and two components of different
classes will generate different trees.
• It is possible to provide a unique key for
elements that is stable across different renders.
List-wise Diff
Keys
renderA: <div><span key="first">first</span></div>
renderB: <div><span key="second">second</span><span
key="first">first</span></div>
=> [insertNode <span>second</span>]
Code
If you specify a key, React is now able to find insertion,
deletion, substitution and moves in O(n) using a hash table.
Trade-offs
• The algorithm will not try to match sub-trees of different
components classes. If you see yourself alternating
between two components classes with very similar
output, you may want to make it the same class. In
practice, we haven't found this to be an issue.
Trade-offs
• The algorithm will not try to match sub-trees of different
components classes. If you see yourself alternating
between two components classes with very similar
output, you may want to make it the same class. In
practice, we haven't found this to be an issue.
• If you don't provide stable keys (by using
Math.random() for example), all the sub-trees are
going to be re-rendered every single time. By giving
the users the choice to choose the key, they have the
ability to shoot themselves in the foot.
Where to go from here?
& Acknowledgements
• Official Documentation: https://
facebook.github.io/react/
• React’s diff algorithm: http://
calendar.perfplanet.com/2013/diff/
• React Demystified: http://blog.reverberate.org/
2014/02/react-demystified.html