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

[core] Replace indexOf with includes #42883

Merged
merged 6 commits into from
Aug 19, 2024
Merged

Conversation

k-rajat19
Copy link
Contributor

Fixes #42826

@aarongarciah aarongarciah added core Infrastructure work going on behind the scenes scope: code-infra Specific to the core-infra product labels Jul 8, 2024
@aarongarciah aarongarciah self-requested a review July 8, 2024 06:21
Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

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

Tests are failing. Some conditions must be negated depending on whether the searched term should be present or not:

  • arr.indexOf(term) === -1 -> !array.includes(term)
  • arr.indexOf(term) !== -1 -> array.includes(term)

The same applies when using .indexOf on strings.

docs/pages/versions.js Outdated Show resolved Hide resolved
@aarongarciah aarongarciah removed the scope: code-infra Specific to the core-infra product label Jul 8, 2024
@aarongarciah aarongarciah changed the title refactor: use built-in includes method instead of index [core] Replace indexOf with includes Jul 8, 2024
@mui-bot
Copy link

mui-bot commented Jul 9, 2024

@aarongarciah aarongarciah requested a review from a team July 9, 2024 08:38
Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

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

Looks good after the last changes, but I'd like an additional review from @mui/code-infra since this PR touches many different areas.

Copy link
Member

@Janpot Janpot left a comment

Choose a reason for hiding this comment

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

I'm not combing through each and every individual line change, but I'm good with the approach. Two things to note:

  • Are we aware of any microbenchmarks that compare both approaches?
  • In a similar fashion we could look into replacing someString.indexOf(someOtherString) === 0 with someString.startsWith(someOtherString). Perhaps we're also using some other pattern of indexOf that can be replaced with endsWith?

edit: Just doing a regex search (.indexOf\([^)]) and it seems like I can still find occurences not covered by this PR

@romgrk
Copy link
Contributor

romgrk commented Jul 15, 2024

Looking through the changes here I don't think any of them is important enough to worry about the performance of those methods.

@Janpot
Copy link
Member

Janpot commented Jul 15, 2024

Looking through the changes here I don't think any of them is important enough to worry about the performance of those methods.

Absolutely, and my expectation is that there won't be any significant impact, It's not a blocker, I'm just curious.
A while ago I saw some benchmarks for the startsWith case that suggested it's slower than indexOf(...) === 0 which is really counterintuitive imo. Haven't dug into this yet to find out what's going on there though, broken benchmark or something I misunderstand about js?

@aarongarciah
Copy link
Member

@Janpot

Are we aware of any microbenchmarks that compare both approaches?

The general discourse seems that .includes might be a bit slower, but the difference is so tiny that it's not worth caring about. These are old links but probably still apply:

In a similar fashion we could look into replacing someString.indexOf(someOtherString) === 0 with someString.startsWith(someOtherString).

Great idea. @k-rajat19 could you update the PR converting someString.indexOf(someOtherString) === 0 to someString.startsWith(someOtherString). You can do a regex search to find all occurrences. I found 20 of them.

Screenshot 2024-07-15 at 11 17 29

Perhaps we're also using some other pattern of indexOf that can be replaced with endsWith?

I did a quick search and couldn't find this pattern.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jul 30, 2024
@aarongarciah
Copy link
Member

Hey @k-rajat19, are you available to make the requested changes?

@k-rajat19
Copy link
Contributor Author

Sorry I forgot about this, I will update it soon

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 19, 2024
Copy link
Member

@aarongarciah aarongarciah left a comment

Choose a reason for hiding this comment

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

Thanks @k-rajat19! As @Janpot pointed out there are lots of .indexOf occurrences not covered by this PR. We can merge this one and if you are up for it, feel free to open an additional PRs to cover all occurrences.

@aarongarciah aarongarciah merged commit f1a5b88 into mui:next Aug 19, 2024
21 checks passed
@k-rajat19 k-rajat19 deleted the patch-42826 branch August 19, 2024 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[core] Use Array.includes instead of index checking
5 participants