Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Optimize this code

0 views
Skip to first unread message

Waseem

unread,
Jul 9, 2009, 5:26:45 PM7/9/09
to
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[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);
}

Peter May

unread,
Jul 10, 2009, 2:07:44 AM7/10/09
to
Waseem pisze:

Try to optimize loops. See:
http://blogs.sun.com/greimer/resource/loop-test.html

--
Peter

Sabine Dinis Blochberger

unread,
Jul 10, 2009, 4:39:47 AM7/10/09
to
Waseem wrote:

> 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>

Dr J R Stockton

unread,
Jul 10, 2009, 10:40:50 AM7/10/09
to
In comp.lang.javascript message <434c1528-49e1-4590-aa03-c580f65fbeac@o7
g2000yqb.googlegroups.com>, Thu, 9 Jul 2009 14:26:45, Waseem
<waseem...@gmail.com> 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.

Johannes Baagoe

unread,
Jul 10, 2009, 2:25:48 PM7/10/09
to
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.

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

Dr J R Stockton

unread,
Jul 11, 2009, 9:30:28 AM7/11/09
to
In comp.lang.javascript message <B4CdnYTDa4QxGsrXnZ2dnUVZ8r9i4p2d@gigane
ws.com>, Fri, 10 Jul 2009 13:25:48, Johannes Baagoe <baa...@baagoe.com>
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 (&#0307; &#0322;), 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)

Thomas 'PointedEars' Lahn

unread,
Jul 12, 2009, 12:53:35 PM7/12/09
to
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

ne...@chthonic.f9.co.uk

unread,
Jul 13, 2009, 9:35:01 AM7/13/09
to

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

Johannes Baagoe

unread,
Jul 13, 2009, 11:37:14 AM7/13/09
to
Johannes Baagoe :

>> 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 (&#0307; &#0322;), 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

Johannes Baagoe

unread,
Jul 13, 2009, 12:18:57 PM7/13/09
to
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.

--
Johannes

David Mark

unread,
Jul 14, 2009, 2:40:28 AM7/14/09
to

Why assume? The specs are publicly available.

0 new messages