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[i] = tmp.split("", 1);
tmp = tmp.replace(ls[i], "");
if(ls[i] == ",") {
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);
}
Try to optimize loops. See:
http://blogs.sun.com/greimer/resource/loop-test.html
--
Peter
> Hi, a little new to doing js. made a little script - wondering if it
> can be optimized further and if so how ?
>
First of all, are you experiencing performance problems? If not, leave
it alone (focus on writing readable code). If yes, do your best to pin
point the location of the issue (bottleneck).
Keywords "premature optimization"
<http://en.wikipedia.org/wiki/Optimization_(computer_science)#When_to_optimize>
<http://www.c2.com/cgi/wiki?PrematureOptimization>
> /* 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.
> 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.
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.
--
Johannes
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)
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
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
>> 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.
I am not sure I would qualify any language as more "reasonable" than
others, but even for those using mostly Latin letters, the list is
rather long, cf. http://en.wikipedia.org/wiki/List_of_Latin_letters
> 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.
U+0133 LATIN SMALL LIGATURE IJ might even be *two* characters, I don't
know how Dutch users feel about it. Or, for that matter, about the
"y" which seems to provide an alternative in some contexts.
In French, there is a clear distinction between "œ" and "oe" (one
writes "cœur", but never "cœfficient"). Still, I suspect that most
French speakers are unaware of that fact until it is pointed out,
and would spontaneously count U+0153 LATIN SMALL LIGATURE OE as two
letters. As indeed they should, arguably, in the case of U+FB00 LATIN
SMALL LIGATURE FF - even three for U+FB03 LATIN SMALL LIGATURE FFI !
Anyway, according to Unicode, each of those is *one* Lowercase Letter.
Right or wrong, that provides perhaps the most sensible standard for
programmers.
> Or \u0138, \u0140.
I'm not sure there is still anywhere those may be "at home".
> 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.
My point exactly : if one wants to write a script that counts lowercase
letters wherever it makes sense, and is ready to accept Unicode's
perhaps debatable view about what a letter is, it is nice to be able to
rely on at least a Basic Unicode Support Level 1 in Regular Expressions
(http://unicode.org/reports/tr18/), including the standard Properties
like Lowercase Letter.
Languages like Perl do a rather decent job, which shows it is
possible. Present implementations of javascript don't, to say the
least, notwithstanding the fact that ECMAScript Strings are expressly
allowed to contain any Unicode character.
The same applies to case conversion, etc; e.g., toLowerCase can only
be relied on if the String is in English, or only contains ASCII
characters for some other reason.
--
Johannes
>> 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.
--
Johannes
Why assume? The specs are publicly available.