John Halbert
John Halbert

Reputation: 1350

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.

Upvotes: 5

Views: 1640

Answers (1)

azium
azium

Reputation: 20624

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.

Upvotes: 1

Related Questions