Make This Subroutine Faster

V

vunet.us

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)
{
case "1": doOne();
case "2": doTwo();
case "N": doN();
}
}

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

Lee

(e-mail address removed) 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)
{
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.


--
 
V

vunet.us

(e-mail address removed) 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)
{
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.
 
D

Darko

(e-mail address removed) 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)
{
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...?
 
V

vunet.us

(e-mail address removed) 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)
{
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
 
R

Randy Webb

Darko said the following on 6/20/2007 12:16 PM:
(e-mail address removed) 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)
{
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
"myVariableWithAReallyLongNameJustForTestingToSeeIfAScriptWillExecuteItSlowerBecauseItHasALongerNameThatRandyMadeUpJustToComeUpWithAReallyLongNameToShowThatTheLengthOfAVariableNameHasNoRealImpactOnJSExecutionTimeAndTheSameIsTrueForFunctionNamesAndIsEasyEnoughToTest"
 
J

Jang

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)
{
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]();
}
}
 
L

Lee

Jang 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)
{
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?


--
 
S

seani

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)
{
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?
 
J

Jang

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

vunet.us

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)
{
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
 
R

Roman

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)
{
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); }
 
R

Roman

Roman 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)
{
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top