4 ms·
Perhaps, (this.lastIndexOf(s, 0) === 0) will work faster for startsWith(s), because it doesn't create new string just for the sole purpose of using it in a sing
by valyala 15y ago
Perhaps, (this.lastIndexOf(s, 0) === 0) will work faster for startsWith(s), because it doesn't create new string just for the sole purpose of using it in a single comparison. What if s contains 1M 'a' chars, while this contains 1M 'b' chars? Your function will naturally make a copy of this, while it could only compare the first character in order to determine the result.
The same applies to endWith() - { var pos = this.length - s.length; return (pos >= 0 && this.indexOf(s, pos) == pos); } will probably work faster in most cases.