Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big O / Time Complexity for React Lifecyle

What's the time complexity for the React component lifecycle?

I was reading a post here recently that suggested where a closure was called for, it could be a good idea to use a component instead. In term of vanilla JavaScript, time complexity should be the same or similar since in the end both are functions. Space complexity may be larger for the component. But the component has to go through the React lifecycle, correct? Which I assume adds overhead.

like image 703
John Halbert Avatar asked Nov 08 '22 10:11

John Halbert


1 Answers

My intuition says that the complexity is O(n) A quick peak at the reconciliation docs seems to support my intuition.

Every operation, lifecycle or not, will at most happen just once per node in the tree, so upper limit of O(n * ops) However in practice it is likely way less since components don't always render thanks to shouldComponentUpdate, and other sub-tree rendering techniques.

like image 166
azium Avatar answered Nov 14 '22 21:11

azium