-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Rule proposal: Prefer String#startsWith()
and String#endsWith()
#285
Comments
I will work on this after accepted by the core team. |
I don't necessarily agree that |
@bradzacher I'm not convinced by a micro-benchmark. Whether this actually matters in production code is unclear. Also, |
I went and found that benchmark to confirm my thinking was correct. charAt/index access should dive straight into the memory via pointer arithmetic - i.e. should be a near free, super cheap operation. The overhead of that clearly adds up! You're right though that they might consider adding a special code path for starts/endsWith being called with a single character. We would probably want to add some settings to allow people to customise what's checked. |
https://twitter.com/ryzokuken/status/1096281036403691520 :)
Sure. I think |
Haha! I didn't know you had a direct line to the V8 devs :P |
The rule would catch the following cases:
.startsWith
instead of:foo.indexOf('bar') === 0
foo[0] === 'b'
foo.charAt(0) === 'b'
(andfoo.charAt()
)foo.slice(0, 2) === 'bar'
(and.substring
&.substr
)/^bar/.test(foo)
(and.match()
).endsWith
instead of:.lastIndexOf
foo[foo.length - 1] === 'r'
foo.charAt(foo.length - 1) === 'b'
foo.slice(0, -3) === 'bar'
(and.substring
&.substr
)/bar$/.test(foo)
(and.match()
)The regex case is especially useful as I've seen a lot of regexes used in this scenario.
The text was updated successfully, but these errors were encountered: