473,468 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Make This Subroutine Faster

Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
....................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}

But I doubt this will speed up, or will it? What would you suggest?

Jun 20 '07 #1
12 1615
Lee
vu******@gmail.com said:
>
Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}

But I doubt this will speed up, or will it? What would you suggest?
That won't even do what you want.

You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn't have to repeat the getElementById call:

var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }

With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.
--

Jun 20 '07 #2
On Jun 20, 11:16 am, Lee <REM0VElbspamt...@cox.netwrote:
vunet...@gmail.com said:


Is there a suggestion I can make this code run faster:
if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }
It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:
var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}
But I doubt this will speed up, or will it? What would you suggest?

That won't even do what you want.

You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn't have to repeat the getElementById call:

var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }

With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.

--
Thanks. Your point is good. However, my doOne() and doTwo are so
different, that combining them will not change much. Besides, they are
reused on other parts of my app. I was really hoping to simplify
document.getElementById("N") routine because there are too many of
those... But I will reconsider the logic based on your suggestion.

Jun 20 '07 #3
On Jun 20, 5:38 pm, vunet...@gmail.com wrote:
On Jun 20, 11:16 am, Lee <REM0VElbspamt...@cox.netwrote:
vunet...@gmail.com said:
>Is there a suggestion I can make this code run faster:
>if(document.getElementById("1")){ doOne(); }
>if(document.getElementById("2")){ doTwo(); }
>...................
>if(document.getElementById("n")){ doN(); }
>It is a simplified version above. There is a large number of these
>repetitive actions. So I wanted to change them for:
>var arr = new Array("1","2","N");
>for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
>}
>But I doubt this will speed up, or will it? What would you suggest?
That won't even do what you want.
You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn't have to repeat the getElementById call:
var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }
With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.
--

Thanks. Your point is good. However, my doOne() and doTwo are so
different, that combining them will not change much. Besides, they are
reused on other parts of my app. I was really hoping to simplify
document.getElementById("N") routine because there are too many of
those... But I will reconsider the logic based on your suggestion.
Are those id's really 1, 2, 3, ... n, or is it just an illustration?
Is it actually 'a', 'blah', 'trt', e.t.c.? Further, are these
functions really named doOne, doTwo etc. and if they are, could they
be renamed into do1, do2, ... , don? Maybe in that case you could do
some improvement on code simplicity and brevity, but then again that
wouldn't be much of a speed-up itself. What you're asking here is how
to replace document.getElementById() with something faster - you can't
really get that, but what you could do, for an example, is put the
same 'name' attribute into all of the elements you are interested in,
and then do document.getElementsByName() in order to process only
these, according to the id you find in them, so you don't call
document.getElementById() for each element. That could maybe improve
the speed of the code. Having nothing more than an illustration leaves
us with no genius ideas...

A question for those having experience with it - does the text-size
has any impact on code-execution time? Id est, does it matter if the
variables are named like oddGroupDescriptionSmall or ogds? Javascript
is a script-language, executing the code as it finds it, so I thought
maybe the speed might depend on the time the js engine needs to parse
the code...?

Jun 20 '07 #4
On Jun 20, 12:16 pm, Darko <darko.maksimo...@gmail.comwrote:
On Jun 20, 5:38 pm, vunet...@gmail.com wrote:
On Jun 20, 11:16 am, Lee <REM0VElbspamt...@cox.netwrote:
vunet...@gmail.com said:
Is there a suggestion I can make this code run faster:
if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }
It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:
var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}
But I doubt this will speed up, or will it? What would you suggest?
That won't even do what you want.
You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn't have to repeat the getElementById call:
var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }
With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.
--
Thanks. Your point is good. However, my doOne() and doTwo are so
different, that combining them will not change much. Besides, they are
reused on other parts of my app. I was really hoping to simplify
document.getElementById("N") routine because there are too many of
those... But I will reconsider the logic based on your suggestion.

Are those id's really 1, 2, 3, ... n, or is it just an illustration?
illustration only...
Is it actually 'a', 'blah', 'trt', e.t.c.? Further, are these
functions really named doOne, doTwo etc. and if they are, could they
be renamed into do1, do2, ... , don?
sorry, i cannot do that..
Maybe in that case you could do
some improvement on code simplicity and brevity, but then again that
wouldn't be much of a speed-up itself. What you're asking here is how
to replace document.getElementById() with something faster - you can't
really get that, but what you could do, for an example, is put the
same 'name' attribute into all of the elements you are interested in,
and then do document.getElementsByName() in order to process only
these, according to the id you find in them, so you don't call
document.getElementById() for each element. That could maybe improve
the speed of the code. Having nothing more than an illustration leaves
us with no genius ideas...

A question for those having experience with it - does the text-size
has any impact on code-execution time? Id est, does it matter if the
variables are named like oddGroupDescriptionSmall or ogds?
I'd like to know that too...

Javascript
is a script-language, executing the code as it finds it, so I thought
maybe the speed might depend on the time the js engine needs to parse
the code...?
thanks

Jun 20 '07 #5
Darko said the following on 6/20/2007 12:16 PM:
On Jun 20, 5:38 pm, vunet...@gmail.com wrote:
>On Jun 20, 11:16 am, Lee <REM0VElbspamt...@cox.netwrote:
>>vunet...@gmail.com said:
Is there a suggestion I can make this code run faster:
if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }
It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:
var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}
But I doubt this will speed up, or will it? What would you suggest?
That won't even do what you want.
You could probably get a much more significant speedup by rethinking
whatever it is that you do differently for each element. At the
very least, if, for example, doOne() requires a reference to the
element with id of "1", you should pass that reference to it, so
it doesn't have to repeat the getElementById call:
var id;
if(id=document.getElementById("1")){ doOne(id) }
if(id=document.getElementById("2")){ doOne(id) }
With a little thought, you can probably combine all of your
doOne(), doTwo(),... functions into a single function.
--
Thanks. Your point is good. However, my doOne() and doTwo are so
different, that combining them will not change much. Besides, they are
reused on other parts of my app. I was really hoping to simplify
document.getElementById("N") routine because there are too many of
those... But I will reconsider the logic based on your suggestion.

Are those id's really 1, 2, 3, ... n, or is it just an illustration?
Is it actually 'a', 'blah', 'trt', e.t.c.? Further, are these
functions really named doOne, doTwo etc. and if they are, could they
be renamed into do1, do2, ... , don? Maybe in that case you could do
some improvement on code simplicity and brevity, but then again that
wouldn't be much of a speed-up itself. What you're asking here is how
to replace document.getElementById() with something faster - you can't
really get that, but what you could do, for an example, is put the
same 'name' attribute into all of the elements you are interested in,
and then do document.getElementsByName() in order to process only
these, according to the id you find in them, so you don't call
document.getElementById() for each element. That could maybe improve
the speed of the code. Having nothing more than an illustration leaves
us with no genius ideas...

A question for those having experience with it - does the text-size
has any impact on code-execution time? Id est, does it matter if the
variables are named like oddGroupDescriptionSmall or ogds? Javascript
is a script-language, executing the code as it finds it, so I thought
maybe the speed might depend on the time the js engine needs to parse
the code...?
It doesn't make a hill of beans if you name your variable x or you name
it
"myVariableWithAReallyLongNameJustForTestingToSeeI fAScriptWillExecuteItSlowerBecauseItHasALongerName ThatRandyMadeUpJustToComeUpWithAReallyLongNameToSh owThatTheLengthOfAVariableNameHasNoRealImpactOnJSE xecutionTimeAndTheSameIsTrueForFunctionNamesAndIsE asyEnoughToTest"

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 20 '07 #6
On Jun 20, 10:34 am, vunet...@gmail.com wrote:
Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}

}

But I doubt this will speed up, or will it? What would you suggest?

how about:
var funcs = [doOne, doTwo, ... , doN];

doMe(2, funcs); // will execute doTwo;

// if you want to execute all of them depending if 1,2..,n exists
for (var i n funcs) {
doMe(i, funcs);
}

function doMe(index, funcs) {
if (document.getElementById(index)) {
funcs[index + 1]();
}
}

Jun 21 '07 #7
Lee
Jang said:
>
On Jun 20, 10:34 am, vunet...@gmail.com wrote:
>Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}

}

But I doubt this will speed up, or will it? What would you suggest?


how about:
var funcs = [doOne, doTwo, ... , doN];

doMe(2, funcs); // will execute doTwo;

// if you want to execute all of them depending if 1,2..,n exists
for (var i n funcs) {
doMe(i, funcs);
}

function doMe(index, funcs) {
if (document.getElementById(index)) {
funcs[index + 1]();
}
}
Certainly that involves less code, but why do you think it's faster?
--

Jun 21 '07 #8
On Jun 20, 3:34 pm, vunet...@gmail.com wrote:
Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}

}

But I doubt this will speed up, or will it? What would you suggest?

Is this code executed one (onload for example) or many times in
response to user actions etc?

Jun 21 '07 #9
On Jun 21, 3:55 am, Lee <REM0VElbspamt...@cox.netwrote:
>
Certainly that involves less code, but why do you think it's faster?

--
I think the original poster wanted it simplifed (based on the thread),
not faster necessarily.
I forgot how arrays are implemented in JavaScript, but if they are
implemented correctly the look up should be O(1) time instead of O(n).
Which should make it faster. Also, less code = smaller download time;
therefore it should feel a bit "faster."

If the author really wanted to make the code go faster, theres not
much he can do IMO. If the code is dragging maybe there is a
bottleneck somewhere in his doOne, doTwo functions instead of the code
shown.

Jun 21 '07 #10
On Jun 21, 12:55 am, Jang <jangc...@gmail.comwrote:
On Jun 20, 10:34 am, vunet...@gmail.com wrote:
Is there a suggestion I can make this code run faster:
if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }
It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:
var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}
But I doubt this will speed up, or will it? What would you suggest?

how about:

var funcs = [doOne, doTwo, ... , doN];

doMe(2, funcs); // will execute doTwo;

// if you want to execute all of them depending if 1,2..,n exists
for (var i n funcs) {
doMe(i, funcs);

}

function doMe(index, funcs) {
if (document.getElementById(index)) {
funcs[index + 1]();
}

}
I like this idea, thanks. If not much of an execution speed, at least
it is a cleaner code. I was not sure one can run function like
funcs[index + 1]();
Thanks

Jun 21 '07 #11
vu******@gmail.com wrote:
Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}

But I doubt this will speed up, or will it? What would you suggest?
Every document.getElementById will result in two lookups, one for
"document" and second for getElementById. Lookups are fast but you can
save a microsecond or two. Then as others have already pointed out that
you may want to pass element object to the callee:

var myGetById = document.getElementById;

if(elem=myGetById("1")) { doOne(id); }
if(elem=myGetById("2")) { doTwo(id); }

/* ... */

if(elem=myGetById("N")) { doN(id); }

--
Roman Ziak
www.dipmicro.com
Jun 21 '07 #12
Roman wrote:
vu******@gmail.com wrote:
>Is there a suggestion I can make this code run faster:

if(document.getElementById("1")){ doOne(); }
if(document.getElementById("2")){ doTwo(); }
...................
if(document.getElementById("n")){ doN(); }

It is a simplified version above. There is a large number of these
repetitive actions. So I wanted to change them for:

var arr = new Array("1","2","N");
for(var i=0;i<arr.length;i++){
switch(arr[i])
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}

But I doubt this will speed up, or will it? What would you suggest?

Every document.getElementById will result in two lookups, one for
"document" and second for getElementById. Lookups are fast but you can
save a microsecond or two. Then as others have already pointed out that
you may want to pass element object to the callee:

var myGetById = document.getElementById;

if(elem=myGetById("1")) { doOne(id); }
if(elem=myGetById("2")) { doTwo(id); }

/* ... */

if(elem=myGetById("N")) { doN(id); }
"id" argument inside the calls is of course supposed to be "elem".

--
Roman Ziak
www.dipmicro.com
Jun 21 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Farber | last post by:
Not sure if this is the right group for this but anyways... I've got an ASP web application that uses a Visual Basic component to do some work. I instantiate the component in asp and then...
13
by: Woody Splawn | last post by:
From a Winform I am calling the sub routine that follows in a module Public Sub PopulateWithStates(ByVal myform As Contracts, ByVal FieldName As String) Select Case FieldName Case "txtST"...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
10
by: nasau | last post by:
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the...
1
by: peterv6 | last post by:
I'm using a "package" type subroutine, called test_package.pl. I'm calling it from a script called split0.pl. I want to pass the $0 variable, use the subroutine to split out just the filename, and...
4
by: otterbyte | last post by:
Hi, I have a bit of code which is confusing me to no end. Here are the basics: 1) The class module is being used in the module of a form. 2) There is an instance of the object declared at the...
3
by: sangith | last post by:
Hi, I have question on processing the file handle in a subroutine. Here is my program without subroutine: open FH1, "<outfile" or die "cannot open the file for reading: $!\n"; while...
3
shrek123
by: shrek123 | last post by:
How can I pass output of some perl subroutine to a subroutine? I have Subroutine1 and wanna pass the return value of this subroutine as an argument to another subroutine. I tried this; ...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.