lit
lit copied to clipboard
feat(labs/ssr): use RegExp.exec to escape HTML
Switches from using replace(pattern, fn)
to using the exec
method of a RegExp
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)
- Return
str.replace(pattern, fn)
wherefn
returns the replacement character
Algorithm 2 (this PR)
- Match against the string with
pattern.exec(str)
- If no match, return early (no special chars anywhere)
- Otherwise, consume the preceding text and the escaped character
-
exec
the pattern again and repeat this process until there are no matches - If the last match wasn't the end of the string, consume the remaining text of the string
- Return the resulting new string
Algorithm 3 (React)
- Match against the string with
pattern.exec(str)
- If no match, return early (no special chars anywhere)
- Otherwise, consume the preceding text and the escaped character
- Iterate through the remaining characters until we reach another special character (not using regex)
- Repeat from step 3
- Return the resulting new string once we have reached the end
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:
- Particularly terrible with the 3rd string, since it has to iterate through all the remaining characters for no reason whereas algo 2 would've already stopped long ago (since
exec
would've returnednull
) - Fastest at shorter strings which contain many special chars
Algorithm 2:
- Fastest overall
Algorithm 1:
- Slowest overall
The early return probably helps a lot since many strings do not contain special chars.