Skip to content

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

Closed

Conversation

ShuiRuTian
Copy link

@ShuiRuTian ShuiRuTian commented Dec 9, 2024

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

Copy link

vercel bot commented Dec 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2024 7:01am

@facebook-github-bot
Copy link
Contributor

Hi @ShuiRuTian!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@ShuiRuTian ShuiRuTian changed the title perf: remove regexp in function escapeHtml perf(SSR): remove regexp in function escapeHtml Dec 10, 2024
@react-sizebot
Copy link

The size diff is too large to display in a single comment. The GitHub action for this pull request contains an artifact called 'sizebot-message.md' with the full message.

Generated by 🚫 dangerJS against 4828172

@ShuiRuTian
Copy link
Author

Hi @elicwhite, could you help to review this PR?

@gnoff
Copy link
Collaborator

gnoff commented Apr 21, 2025

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!
Josh

@gnoff gnoff closed this Apr 21, 2025
@ShuiRuTian
Copy link
Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants