Bug: String.prototype.endsWith
# Xavier MONTILLET (14 years ago)
[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 :-*
[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 <axel at 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 <axel at 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 >>> axel at rauschma.de >>> twitter.com/rauschma >>> home: rauschma.de >>> blog: 2ality.com >>> >>> >>> >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >>> >> > > -- > Dr. Axel Rauschmayer > > axel at rauschma.de > twitter.com/rauschma > > home: rauschma.de > blog: 2ality.com > > > >
# Rick Waldron (14 years ago)
That jsperf is too noisy to actually be measuring the performance of anything. If you want to test the performance of just the logic in question
- then that should be all that exists.
From a web dev perspective... fallback definitions should always try to
minimize the number of other function calls they make.
That jsperf is too noisy to actually be measuring the performance of anything. If you want to test the performance of just the logic in question - then that should be all that exists. See: http://jsperf.com/endwithlogic >From a web dev perspective... fallback definitions should always try to minimize the number of _other_ function calls they make. Rick On Fri, Sep 23, 2011 at 5:11 PM, Xavier MONTILLET <xavierm02.net at gmail.com>wrote: > [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 <axel at 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 <axel at 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 > >>> axel at rauschma.de > >>> twitter.com/rauschma > >>> home: rauschma.de > >>> blog: 2ality.com > >>> > >>> > >>> > >>> _______________________________________________ > >>> es-discuss mailing list > >>> es-discuss at mozilla.org > >>> https://mail.mozilla.org/listinfo/es-discuss > >>> > >>> > >> > > > > -- > > Dr. Axel Rauschmayer > > > > axel at rauschma.de > > twitter.com/rauschma > > > > home: rauschma.de > > blog: 2ality.com > > > > > > > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110923/d3c65e46/attachment.html>
# Rick Waldron (14 years ago)
Note, next to the "Filter" option, above the chart, click "All"
Note, next to the "Filter" option, above the chart, click "All" On Fri, Sep 23, 2011 at 6:41 PM, Rick Waldron <waldron.rick at gmail.com>wrote: > That jsperf is too noisy to actually be measuring the performance of > anything. If you want to test the performance of just the logic in question > - then that should be all that exists. > > See: http://jsperf.com/endwithlogic > > From a web dev perspective... fallback definitions should always try to > minimize the number of _other_ function calls they make. > > > Rick > > > On Fri, Sep 23, 2011 at 5:11 PM, Xavier MONTILLET <xavierm02.net at gmail.com > > wrote: > >> [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 <axel at 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 <axel at 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 >> >>> axel at rauschma.de >> >>> twitter.com/rauschma >> >>> home: rauschma.de >> >>> blog: 2ality.com >> >>> >> >>> >> >>> >> >>> _______________________________________________ >> >>> es-discuss mailing list >> >>> es-discuss at mozilla.org >> >>> https://mail.mozilla.org/listinfo/es-discuss >> >>> >> >>> >> >> >> > >> > -- >> > Dr. Axel Rauschmayer >> > >> > axel at rauschma.de >> > twitter.com/rauschma >> > >> > home: rauschma.de >> > blog: 2ality.com >> > >> > >> > >> > >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110923/eaef61a5/attachment.html>
# David Herman (14 years ago)
Fixed, thanks.
Dave, digging his way out of a massive backlog...
Fixed, thanks. Dave, digging his way out of a massive backlog... On Sep 23, 2011, at 12:18 PM, Axel Rauschmayer 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 > > axel at rauschma.de > twitter.com/rauschma > > home: rauschma.de > blog: 2ality.com > > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111007/40f0ead4/attachment.html>
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:
true
true
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; };