Closed Thread Icon

Topic awaiting preservation: Which method is optimal Pages that link to <a href="https://ozoneasylum.com/backlink?for=8777" title="Pages that link to Topic awaiting preservation: Which method is optimal" rel="nofollow" >Topic awaiting preservation: Which method is optimal\

 
Author Thread
ZOD
Bipolar (III) Inmate

From:
Insane since: Jun 2002

posted posted 07-24-2003 04:12

I was just reading that JScript processes variable declarations before executing any code. So which construct is optimal?

This -

if (sometest) {
var i=foo;
var j=bar;
do stuff with these vars
}
else {
var x=foobar;
var y=barfoo;
do something else with these vars
}


or this -

var i=foo;
var j=bar;
var x=foobar;
var y=barfoo;
if (sometest) {
do stuff
}
else {
do something else
}


????


synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 07-24-2003 04:40

It only seems logical that declaring variables at the head of the script would be better.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 07-24-2003 04:51

I agree, mate.
I have never saw declaring inside.


Hiroki Kozai

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 07-24-2003 04:59

I agree, variable declarations should be done prior to anything else, unless the logic of your code requires otherwise.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 07-24-2003 05:11

There's probably no difference at all in speed between the two constructs. Of course, it might depend on the individual browser. But even if there is a difference, you'll never notice it.

ZOD
Bipolar (III) Inmate

From:
Insane since: Jun 2002

posted posted 07-25-2003 03:10

Thanks fellas. I moved all my var declarations to the beginning of the script I'm working on and what I didn't move I converted to local vars by creating a few new functions. I did note a slight speed boost but it is a rather large script with a lot of vars. On a normal sized script I probably couldn't tell any difference.

« BackwardsOnwards »

Show Forum Drop Down Menu