Optimize this code

Discussion in 'Javascript' started by Waseem, Jul 9, 2009.

  1. Waseem

    Waseem Guest

    Hi, a little new to doing js. made a little script - wondering if it
    can be optimized further and if so how ?

    here is the script.

    function examine(selector) {
    /*
    if selector = "#new, .newer, newest"
    output will be :
    selections = 3; // there are 2 commas
    s.selects[0] = "#new";
    s.selects[2] = ".newer";
    s.selects[3] = "newest";

    think of it like examining css selections

    #new = 1 selection
    #new, .newer = 2 selections
    #new, .newer, newest = 3 selections
    */
    /* get length of selector */
    var l = selector.length;
    var s = [];
    var ls = new Array();
    /* create a temporary holder for the selector */
    var tmp = selector;
    /* default number of selects is one */
    s.selections = 1;
    /* seperate each select */
    s.selects = new Array();
    /* split each character of the string up to find how many comma
    characters exist
    for eac comma character increment s.selections by 1*/
    for(var i = 0; i < (l - 1);i++) {
    ls = tmp.split("", 1);
    tmp = tmp.replace(ls, "");
    if(ls == ",") {
    s.selections++;
    }
    }
    /* reset the temporary holder */
    tmp = selector;
    /* split each selection into an array */
    for(var j = 0; j < s.selections; j++) {
    s.selects[j] = tmp.split(",", 1);
    tmp = tmp.replace(s.selects[j] + ",", "");
    }
    return (s.selects);
    }
     
    Waseem, Jul 9, 2009
    #1
    1. Advertising

  2. In comp.lang.javascript message <434c1528-49e1-4590-aa03-c580f65fbeac@o7
    g2000yqb.googlegroups.com>, Thu, 9 Jul 2009 14:26:45, Waseem
    <> posted:

    > /* split each character of the string up to find how many comma
    >characters exist
    > for eac comma character increment s.selections by 1*/


    More briefly done with a RegExp as in
    <URL:http://www.merlyn.demon.co.uk/js-valid.htm> ; this counts lower-
    case letters :-

    count = msg.replace(/[^a-z]/g, '').length

    This counts commas by a simpler implementation of the ped3estrian
    approach :-

    count=0 ; j = msg.length
    while (j--) if (msg.charAt(j)==",") count++

    If the first and last characters cannot be commas, and possibly
    otherwise, you can reliably but inefficiently use :-

    msg.split(",").length - 1

    For a minor gain in speed, make the terminating value of a loop zero.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9 Sf3
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Jul 10, 2009
    #2
    1. Advertising

  3. In comp.lang.javascript message <B4CdnYTDa4QxGsrXnZ2dnUVZ8r9i4p2d@gigane
    ws.com>, Fri, 10 Jul 2009 13:25:48, Johannes Baagoe <>
    posted:
    >Dr J R Stockton :
    >
    >> this counts lower-case letters :-

    >
    >> count = msg.replace(/[^a-z]/g, '').length

    >
    >This does too :
    >
    > count = msg.match(/[a-z]/g).length
    >
    >and some might consider it more natural. It is certainly shorter.


    and is not entirely happy if no letters are found.


    >There is an important caveat, though: it counts lowercase *English*
    >letters. If msg == "Où qu'il réside, même aux îles Caïmans, tout
    >Français
    >inscrit au rôle paiera son dû dès Noël", it won't do the job. As far as I
    >know, given the present level of support of Unicode in javascript, there
    >is no easy way to count *all* lowercase characters.


    For reasonable languages, such as Danish and French, it will be simple
    enough to list the extra letters after a-z.

    It seems unlikely that an application would want to count BOTH of /u0133
    and /u0142 (ij ł), though both are, when at home, lower-case
    characters. Or \u0138, \u0140.


    Since (browsers + operating-systems + drivers) are supposed to be able
    to show many unicode glyphs, it ought to be easy enough to include and
    make accessible for each code point a general decryption of its major
    characteristics, encoded into maybe a word. Space, letter, case,
    number, punctuation, symbol, usual direction and a few other properties.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr J R Stockton, Jul 11, 2009
    #3
  4. Dr J R Stockton wrote:
    > Johannes Baagoe posted:
    >> Dr J R Stockton :
    >>> this counts lower-case letters :-
    >>> count = msg.replace(/[^a-z]/g, '').length

    >> This does too :
    >>
    >> count = msg.match(/[a-z]/g).length
    >>
    >> and some might consider it more natural. It is certainly shorter.

    >
    > and is not entirely happy if no letters are found.


    which can be fixed with

    count = (msg.match(/[a-z]/g) || "").length

    or (since String.prototype.match() returns a reference to an augmented Array
    object):

    count = (msg.match(/[a-z]/g) || []).length


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Jul 12, 2009
    #4
  5. Waseem

    9.co.uk Guest

    On Jul 9, 10:26 pm, Waseem <> wrote:
    > Hi, a little new to doing js. made a little script - wondering if it
    > can be optimized further and if so how ?
    >
    > here is the script.
    >
    > function examine(selector) {
    >         /*
    >         if selector = "#new, .newer, newest"
    >         output will be :
    >         selections = 3; // there are 2 commas
    >         s.selects[0] = "#new";
    >         s.selects[2] = ".newer";
    >         s.selects[3] = "newest";
    >
    >         think of it like examining css selections
    >
    >         #new = 1 selection
    >         #new, .newer = 2 selections
    >         #new, .newer, newest = 3 selections
    >         */
    >         /* get length of selector */
    >         var l = selector.length;
    >         var s = [];
    >         var ls = new Array();
    >         /* create a temporary holder for the selector */
    >         var tmp = selector;
    >         /* default number of selects is one */
    >         s.selections = 1;
    >         /* seperate each select */
    >         s.selects = new Array();
    >         /* split each character of the string up to find how manycomma
    > characters exist
    >          for eac comma character increment s.selections by 1*/
    >         for(var i = 0; i < (l - 1);i++) {
    >                 ls = tmp.split("", 1);
    >                 tmp = tmp.replace(ls, "");
    >                 if(ls == ",") {
    >                         s.selections++;
    >                 }
    >         }
    >         /* reset the temporary holder */
    >         tmp = selector;
    >         /* split each selection into an array */
    >         for(var j = 0; j < s.selections; j++) {
    >                 s.selects[j] = tmp.split(",", 1);
    >                 tmp = tmp.replace(s.selects[j] + ",", "");
    >         }
    >         return (s.selects);
    >
    > }
    >
    >


    Am I being extraordinarily thick, or is this not
    equivalent to

    function examine(selector) {
    return selector.split(',');
    }

    (at least in terms of the code presented, which does
    all sorts of odd things before throwing away the things
    it has done)

    Mike
     
    9.co.uk, Jul 13, 2009
    #5
  6. Waseem

    David Mark Guest

    On Jul 13, 12:18 pm, Johannes Baagoe <> wrote:
    > Johannes Baagoe :
    >
    > >>          count = msg.match(/[a-z]/g).length

    >
    > Dr J R Stockton :
    >
    > > is not entirely happy if no letters are found.

    >
    > Damned. You're right. ''.match(/[a-z]/g) returns null.
    >
    > It makes sense, in a way, since null evaluates to false when converted
    > to Boolean, and [] curiously to true. Other languages made me assume
    > that empty Objects (and hence Arrays) would be been treated the same
    > way as empty Strings, with the resulting, simpler semantics of match
    > and exec : return the (possibly empty) Array of all matches.
    >
    > I suppose the moral is, once again, Never assume anything about javascript.


    Why assume? The specs are publicly available.
     
    David Mark, Jul 14, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron

    Warning and how to optimize code

    Aaron, Jan 19, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,247
    =?Utf-8?B?Sm9l?=
    Jan 19, 2006
  2. deepak
    Replies:
    1
    Views:
    288
    Daniel Pitts
    Jan 31, 2007
  3. gtippery
    Replies:
    40
    Views:
    996
  4. kenneth
    Replies:
    15
    Views:
    502
    White Wolf
    Dec 19, 2006
  5. TF
    Replies:
    10
    Views:
    735
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Jun 28, 2007
Loading...

Share This Page