Forum Moderators: open

Message Too Old, No Replies

Using Option Explicit with VBScript

Defining exactly how it helps

         

mattglet

5:05 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was recently given a task to outline exactly HOW the use of Option Explicit improves your code performance. Now, I know WHY to use it (prevent misspelling variables, scope, etc.), I just can't seem to find HOW it helps when pertaining to execution.

For example, I've seen some research showing that when you Dim your variables, it creates a spot in memory for them, and provides an ordinal reference to the compiler -- creating faster code execution.

Can anyone dive a little deeper into this, possibly providing a more thorough explanation? I'm mostly looking for descriptions how the variables are stored into memory/ordinal references/faster execution.

macrost

5:20 pm on Mar 14, 2005 (gmt 0)

10+ Year Member



mattglet,
I just hit msdn, and well, they say nothing about faster execution. It would make sense if running w/ option explicit would reference each Dim'd var into memory, but as it's vbscript, I'm not sure that it would actually do that.

msdn [msdn.microsoft.com]

I'll do some more digging.

Easy_Coder

8:37 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



matt, I believe that the performance comes into play because non declared variables are seen as Object types.

mattglet

10:11 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I could be wrong, but I think that's only in .NET...

In ASP they are seen as variants.

Easy_Coder

1:57 pm on Mar 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ya know matt, I believe that you're right. These are all variant.

Doesn't a variable that isn't of an explicit type get stuffed onto the heap as an object?

i = 10 <-- unknown so i would actually contain reference to a location on the heap where the value exists.

dim i
i = cint(10) <-- known value type that sits on the stack

mattglet

2:52 pm on Mar 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that's what I'm trying to figure out ;)

I just can't seem to find any SOLID data regarding this... Only a few random statements here and there.

aspdaddy

9:53 am on Mar 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




the scripting run time references undeclared variables by name, every time they are used. Declared variables, on the other hand, are assigned an ordinal, either at compile time or run time. Subsequently, declared variables are referenced by this ordinal. Since Option Explicit forces variable declaration, it insures that all variables are declared and thus will be accessed quickly

[msdn.microsoft.com...]