I've made a minor optimization here to improve performance.
In this new code startsWith() can stop checking as soon as it finds a character that doesn't match, while the former equality comparison checks the entire string.
The behavior of startsWith() and the equality comparison operator are equivalent when the lengths of the needle and haystack are the same, which the if block assures us of.
I've made a minor optimization here to improve performance.
In this new code startsWith() can stop checking as soon as it finds a character that doesn't match, while the former equality comparison checks the entire string.
The behavior of startsWith() and the equality comparison operator are equivalent when the lengths of the needle and haystack are the same, which the if block assures us of.