feat(labs/ssr): use RegExp.exec to escape HTML #4627
Draft
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.
Switches from using
replace(pattern, fn)
to using theexec
method of aRegExp
instead.This is draft until someone can decide if its a sensible thing to do 馃槵
basically, i noticed one of the slow downs compared to other libraries in SSR was this function. it seems
replace(pattern, fn)
is much slower than manually iterating over matches.I've tried 3 different algorithms and loosely benchmarked them.
Algorithm 1 (current)
str.replace(pattern, fn)
wherefn
returns the replacement characterAlgorithm 2 (this PR)
pattern.exec(str)
exec
the pattern again and repeat this process until there are no matchesAlgorithm 3 (React)
pattern.exec(str)
Performance
I loosely benchmarked it using 3 strings:
"foo <div> bar bar </div> baz"
"foo bar baz"
"foo <div> some very long string which does not contain any more special chars ... "
Algorithm 3:
exec
would've returnednull
)Algorithm 2:
Algorithm 1:
The early return probably helps a lot since many strings do not contain special chars.