Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Optimize this code

Reply
Thread Tools

Optimize this code

 
 
Waseem
Guest
Posts: n/a
 
      07-09-2009
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);
}
 
Reply With Quote
 
 
 
 
Dr J R Stockton
Guest
Posts: n/a
 
      07-10-2009
In comp.lang.javascript message <434c1528-49e1-4590-aa03-c580f65fbeac@o7
g2000yqb.googlegroups.com>, Thu, 9 Jul 2009 14:26:45, Waseem
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Dr J R Stockton
Guest
Posts: n/a
 
      07-11-2009
In comp.lang.javascript message <B4CdnYTDa4QxGsrXnZ2dnUVZ8r9i4p2d@gigane
ws.com>, Fri, 10 Jul 2009 13:25:48, Johannes Baagoe <(E-Mail Removed)>
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)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-12-2009
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
 
Reply With Quote
 
news@chthonic.f9.co.uk
Guest
Posts: n/a
 
      07-13-2009
On Jul 9, 10:26*pm, Waseem <(E-Mail Removed)> 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[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);
>
> }
>
>


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
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      07-14-2009
On Jul 13, 12:18*pm, Johannes Baagoe <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Help me Optimize my Code TF ASP .Net 10 06-28-2007 06:46 AM
How can i optimize the web services client access code deepak Java 1 01-31-2007 10:11 PM
Hi, how can I optimize the following code? kenneth C++ 15 12-19-2006 12:34 AM
Warning and how to optimize code Aaron ASP .Net 2 01-19-2006 08:02 AM
Please help optimize (and standarize) this code... gtippery C Programming 40 04-03-2005 01:45 AM



Advertisments