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 <[email protected]>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 <[email protected]> > 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 <[email protected]> > 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 > >>> [email protected] > >>> twitter.com/rauschma > >>> home: rauschma.de > >>> blog: 2ality.com > >>> > >>> > >>> > >>> _______________________________________________ > >>> es-discuss mailing list > >>> [email protected] > >>> https://mail.mozilla.org/listinfo/es-discuss > >>> > >>> > >> > > > > -- > > Dr. Axel Rauschmayer > > > > [email protected] > > twitter.com/rauschma > > > > home: rauschma.de > > blog: 2ality.com > > > > > > > > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

