#123 enhancement
Luca Guidi

String#strip perfomances enhancement

Reported by Luca Guidi | May 28th, 2008 @ 11:24 PM | in 1.7

Inspired by Steve Lev's: Faster JavaScript Trim, I wrote an enhanced version of String#strip.

You can find my benchmark results at related post.

Comments and changes to this ticket

  • Juriy Zaytsev

    Juriy Zaytsev May 29th, 2008 @ 12:10 AM

    • State changed from “new” to “enhancement”
  • John-David Dalton

    John-David Dalton May 29th, 2008 @ 04:46 AM

    • Milestone set to 1.7
    • Assigned user set to “Juriy Zaytsev”
  • Steven Levithan

    Steven Levithan May 29th, 2008 @ 11:54 AM

    As I've (indirectly) noted on by blog at http://blog.stevenlevithan.com/a... , this implementation is a bad idea.

  • Luca Guidi

    Luca Guidi May 29th, 2008 @ 12:24 PM

    I tested with the original Steven benchmark suite, the algorithm is faster than others when deal with whitespaces up to 100 (on both string sides). With 1k of whitespaces is slower than others.

    This meant it's not so much scalable, because increase the execution on the whitespaces increasing.

    Now the question is: Is it better to have a general purpose algorithm (scalable) or an implementation which it's faster on short strings (un-scalable)? Do our webapps should handle 2k (1+1) of whitespaces on the client-side?

  • Steven Levithan

    Steven Levithan May 29th, 2008 @ 10:20 PM

    @jodosha, IMHO any non-freaky implementation of trim is fast enough in most cases. It is specifically the edge-cases -- the long strings, the unexpected strings -- where performance matters most.

    A trim method that performs more or less consistently relative to the lengths of the strings it works with is, again IMO, much better than an implementation that has the potential to cause a nasty performance dip and temporarily freeze the page when working with pathological cases.

  • Gregor Tramšak

    Gregor Tramšak March 31st, 2009 @ 10:36 AM

    • Tag set to patched, string, tested

    What about using an OR regex expresion instead of two regex calls. I found this method slower only on firefox, on other browsers its faster.

    @@@ Javascript s.replace(/^\s+|\s+$/g,'');

  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    • Tag changed from patched, string, tested to patched
    • Assigned user cleared.

    [responsible:none bulk edit command]

  • Tisho Georgiev

    Tisho Georgiev March 2nd, 2010 @ 09:45 AM

    • Tag changed from patched to patched, section:lang

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins