-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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(core): use startsWith instead of indexOf 0 #989
Conversation
Here's a perf test for both - not really representative of real-world cases, as you'd never have 100s of characters in a string like this. In positive cases, |
@rigor789 thanks for that. I should have included the benchmark I had, but I actually like the tool you linked better. My assumption is that in these cases, there will most often be small strings that don't start with the prefix. So here is a modified benchmark where |
I'm always a little skeptical with perf tests - both of our benchmarks can go either way between runs. But for the most common strings, it does seem like |
startsWith is not supported on IE though |
@posva looks like it's being used in There are a few more places, but they are mostly in compiler, so less of a concern: |
Right, I saw it being used in runtime-dom, so I thought it was ok to use. I also thought the planned support for IE11, mostly due to Proxies, would be via a build-time flag, so poly-filling for IE support should all only need to happen when that flag is set. Maybe To argue in favor of using |
Good discussions here. I originally was thinking to use |
In a few places,
someString.indexOf(somePrefix) === 0
is used to check if a string starts with some prefix. UsingstartsWith
instead should enable the check to short-circuit in the negative case (indexOf will scan the whole string in the negative case); it also has clearer intent. In one case, checking if something was a v-model listener, the logic was checking existence of a string, but it should always be a prefix, so startsWith seems to make more sense.