By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

Make This Subroutine Faster

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.