Optimize this code

W

Waseem

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);
}
 
D

Dr J R Stockton

In comp.lang.javascript message <434c1528-49e1-4590-aa03-c580f65fbeac@o7
g2000yqb.googlegroups.com>, Thu, 9 Jul 2009 14:26:45, Waseem
/* 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.
 
D

Dr J R Stockton

In comp.lang.javascript message <B4CdnYTDa4QxGsrXnZ2dnUVZ8r9i4p2d@gigane
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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
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
 
N

news

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
 
D

David Mark

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top