Update escaping of attribute and text HTML characters in SSR #4
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.



I noticed when profiling parts of ReactDOM, I noticed that the escaping of HTML characters in SSR was particularly slow. Looking into it more, I changed the implementation to a much faster one that uses
String.prototype.indexOfrather than RegEx (indexOfwas faster in Chrome/Firefox and Safari) and also opted to use a similar implementation to other libraries when it comes to the escaping of characters. I then noticed something interesting: we are escaping characters that no longer need to be escaped.For DOM node attributes, we don't need to escape single quotes or left/right angle brackets. You can try this out yourself in all the different browsers (I never tested IE8). For text nodes, you don't need to escape single or double quotes, again this can be validated in all browsers using the DOM API. You can see for yourself below:
Not only does this improve SSR render performance it also improves hydration when checking if the text nodes have changed, as before we'd mismatch in far more cases where there was a difference in the diffing due to escaped characters of a text node value not matching that of what React derives.
The difference in SSR rendering before (this is using the
hacker-newsbenchmark inscripts):Using the
color-pickerbenchmark from Marko: