Trim functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RobG

    Trim functions

    There is an interseting examination of varions trim functions here:

    <URL: http://blog.stevenlevithan.com/archi...rim-javascript >

    The best all-round function seems to be trim3 when strings have lots
    of leading and trailing whitespace, which is when other functions slow
    down and speed is of most importance.


    --
    Rob

  • Evertjan.

    #2
    Re: Trim functions

    RobG wrote on 03 jun 2008 in comp.lang.javas cript:
    There is an interseting examination of varions trim functions here:
    >
    <URL: http://blog.stevenlevithan.com/archi...rim-javascript >
    >
    The best all-round function seems to be trim3 when strings have lots
    of leading and trailing whitespace, which is when other functions slow
    down and speed is of most importance.
    >
    Interesting!

    While his trim12 is very good at longer strings,
    these do better at short ones:

    function myTrim1 (str) {
    var str = str.replace(/^\s\s*/, '');
    str = str.replace(/\s\s*$/, '');
    };

    function trim1 (str) {
    var str = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    };

    His trim1 taking nearly TWICE the time myTrim1 takes in IE7 and FF2
    independent of string length!

    Why would that be?


    =============== =======

    btw: Leaving of the ;;; does gain round 10%.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Evertjan.

      #3
      Re: Trim functions

      Evertjan. wrote on 03 jun 2008 in comp.lang.javas cript:
      function myTrim1 (str) {
      var str = str.replace(/^\s\s*/, '');
      str = str.replace(/\s\s*$/, '');
      >};
      >
      function trim1 (str) {
      var str = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
      >};
      >
      His trim1 taking nearly TWICE the time myTrim1 takes in IE7 and FF2
      >
      Forget that, I mixed them up, it is the other, more logical, way around.

      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      Working...