[Nope, wasn't deliberate]

I tested and both are as fast apparently but doing it the "hard" way
seems slow. But maybe that's because I didn't do it properly.

And a function isAt could be useful. But it needs another name :-*


http://jsperf.com/endswith

On Fri, Sep 23, 2011 at 10:22 PM, Axel Rauschmayer <a...@rauschma.de> wrote:
> [Was not-cc-ing es-discuss deliberate? Feel free to forward this email if it 
> wasn’t.]
>
> Creating a substring isn’t very efficient, either. But I get your point: Why 
> search the whole string, when it is enough to check a single position?
>
> A better solution might be to create a helper method
>    String.prototype.isAt(substr, pos)
> that returns true if substr is found at position pos in "this".
>
> This helper method could be used to implement endsWith and startsWith.
>
> However, I doubt that the goal for the code was efficiency.
>
> On Sep 23, 2011, at 22:12 , Xavier MONTILLET wrote:
>
>> Why doesn't it simply do
>>
>> String.prototype.endsWith = function(s) {
>>    return this.substring( this.length - s.length ) === String(s);
>> };
>>
>> ?
>>
>> I mean lastIndexOf must be much slower...
>>
>> On Fri, Sep 23, 2011 at 9:18 PM, Axel Rauschmayer <a...@rauschma.de> wrote:
>>> http://wiki.ecmascript.org/doku.php?id=harmony:string_extras
>>> I’ve found a small bug:
>>>
>>> String.prototype.endsWith = function(s) {
>>>
>>>    var t = String(s);
>>>    return this.lastIndexOf(t) === this.length - t.length;
>>> };
>>>
>>> Interaction:
>>>
>>>> "".endsWith("/")
>>> true
>>>> "#".endsWith("//")
>>> true
>>>> "##".endsWith("///")
>>> true
>>>
>>> Fix (e.g.):
>>>
>>> String.prototype.endsWith = function(s) {
>>>    var t = String(s);
>>>
>>>    var index = this.lastIndexOf(t)
>>>    return index >= 0 && index === this.length - t.length;
>>> };
>>>
>>> --
>>> Dr. Axel Rauschmayer
>>> a...@rauschma.de
>>> twitter.com/rauschma
>>> home: rauschma.de
>>> blog: 2ality.com
>>>
>>>
>>>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss@mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>>
>>
>
> --
> Dr. Axel Rauschmayer
>
> a...@rauschma.de
> twitter.com/rauschma
>
> home: rauschma.de
> blog: 2ality.com
>
>
>
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to