Skip to content
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: replace endsWith with === #12539

Merged
merged 5 commits into from Mar 23, 2023
Merged

perf: replace endsWith with === #12539

merged 5 commits into from Mar 23, 2023

Conversation

sun0day
Copy link
Member

@sun0day sun0day commented Mar 22, 2023

Description

Similar to #12531, this PR uses === instead of endsWith in some hot extensions.

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@stackblitz
Copy link

stackblitz bot commented Mar 22, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@bluwy
Copy link
Member

bluwy commented Mar 23, 2023

I'm not sure about this. I'd prefer if it indexes one char only, when searching longer words I think endsWith or startsWith is the right tool for it.

@patak-dev
Copy link
Member

Did you measure the perf diff on this one @sun0day? I also think that this is going too far. I think we better keep using endsWidth here.

@sun0day
Copy link
Member Author

sun0day commented Mar 23, 2023

I only tested the js code performance between str.endsWith and str[len-1] === 'x' && str[len-2]==='y', I haven't tested in a real app since I didn't find a proper one.

The thing is that plain === is 60%-70% faster than endsWith in the js perf test. It's really an attractive number. I am not gonna replace all the endsWith in the codebase but replace some hot extensions' judgment.

But also I know that endsWith is not the bottleneck and it may only improve a little perf even if replace all the endsWith in the real app. I am fine with indexing one char only

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to the next major where we can use String.prototype.at to make these more compact.

@bluwy bluwy enabled auto-merge (squash) March 23, 2023 12:24
@bluwy bluwy merged commit 7eb52ec into vitejs:main Mar 23, 2023
12 checks passed
@robpalme
Copy link

Thank you for identifying this performance issue. I have reported it to the V8 project.

@patak-dev
Copy link
Member

Thanks @robpalme 🙏🏼

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

Successfully merging this pull request may close these issues.

None yet

4 participants