-
Notifications
You must be signed in to change notification settings - Fork 48.8k
perf(SSR): remove regexp in function escapeHtml #31706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(SSR): remove regexp in function escapeHtml #31706
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Hi @ShuiRuTian! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Hi @elicwhite, could you help to review this PR? |
Hi @ShuiRuTian If we are going to make a perf related changed we need to actually make sure it's a perf improvement. Since this is an SSR pathway we're interested in Node performance and edge runtimes primarily. Additionally we can't only test on very short strings and repeated strings because the JS VMs do some tricky things to try to optimize operations that are repeated frequently. From my own testing (nodejs) using some small and some very long strings it seems that the regex impl comes out ahead. This is especially true when the long string doesn't actually have any of the replaced characters. I'm not a benching expert but I don't see compelling reasons to make this change though if you can provide a rationale with reproducible benchmarks across a realistic set of strings I'd reconsider. At the moment though I think we will leave this as is. Feel free to reopen if you want to revisit with new benchmark data. Thanks! |
Thanks! I did some more benchmark and find the result is just like you mentioned. It's indeed strange. I will try to do more benchmark... |
Summary
Regular expression is slow, removing them in escapeHtml should make things faster
I did similar change in vue at first: vuejs/core#12507 (comment)
Then I took a look at react and found vue copied react's implementation, then why not let react have this improvement too?
How did you test this change?
Some simple benchmark by me and @Justineo
https://jsbench.me/rqm4ehfndc/1, shows for simple cases, it's 2x~3x faster
https://jsben.ch/pbGau, shows for random string, it's 1.1x faster
Also cc @aickin, who refactor escapeHtml 8 years ago, maybe you might have interest in this PR too