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

Add replacement for String.startsWith #100

Open
jfmengels opened this issue Aug 16, 2022 · 1 comment · May be fixed by #103
Open

Add replacement for String.startsWith #100

jfmengels opened this issue Aug 16, 2022 · 1 comment · May be fixed by #103

Comments

@jfmengels
Copy link
Contributor

jfmengels commented Aug 16, 2022

I noticed that String.startsWith can be very slow for large strings when the str does not contain the prefix. The reason being the function actually does a str.indexOf call which will go through the entire function as long as it doesn't find anything.

So I tried benchmarking alternatives.

I tried using the ES2015 startsWith, which is by far faster but doesn't work on IE.

And I also tried another version where we slice the string and compare that to the prefix.

Benchmark

Chrome

Firefox


What do you think of the idea of replacing String.startsWith? Does that sound good?

Maybe we can do something faster in JS where we compare every character one by one.

I also think similar improvements can be made for String.endsWith.

@jfmengels
Copy link
Contributor Author

Maybe we can do something faster in JS where we compare every character one by one.

I just tested that and that is by far the fastest (even faster than the native startsWith in FF). The screenshots above now reflect that.

@jfmengels jfmengels linked a pull request Aug 17, 2022 that will close this issue
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 a pull request may close this issue.

1 participant