Improved hydration performance by less reordering of nodes #6392
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As lots of issues (e.g. #1067, #6204) have discussed, there are more DOM changes than one might expect during hydration, which reduces performance.
This is an attempt at significantly reducing the number of DOM changes with a quite minor change to the code. In my tests, it speeds up hydration by around 25%.
As far as I can tell, the issue with the DOM manipulation during hydration is not so much that nodes are removed and recreated (because they generally are recycled), as that during the mount phase we will append them to their parent in order, which actually causes them to reorder:
If you have the child nodes A and B present in the SSR HTML and hydration starts by appending A, A will move to the end and the order becomes B followed by A. It will be reversed again when B is appended, thereby giving the desired order. These reorderings are quite slow.
This PR changes that by:
In actually hydration, the picture is muddied by various less straightforward cases, but in 7 cases out of 8 in my tests, the algorithm above was able to find find the correct next node and therefore skip a DOM operation.
Before submitting the PR, please make sure you do the following
Tests
npm test
and lint the project withnpm run lint