Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JScript equivalent to 'Option Explicit'?

30 views
Skip to first unread message

Jan Bares

unread,
Dec 8, 1999, 3:00:00 AM12/8/99
to
I cannot believe that I cannot trace variables that were not declared. How
do you solve this common source of bugs in JScript?

Thanks, Jan

--

Jan Bares
(remove no.spam from my email address)
JPCAD Graphics Engine developer, surf to http://www.antek.cz
PGP ID: 0x6BFE492A

Jim Ley

unread,
Dec 8, 1999, 3:00:00 AM12/8/99
to

Jan Bares <jan....@antek.cz.no.spam> wrote in message
news:eXGLSgZQ$GA.254@cppssbbsa04...

> I cannot believe that I cannot trace variables that were not declared. How
> do you solve this common source of bugs in JScript?

You take care.. I don't know of any other method, of course I've never looked
as it's not been a problem for me, some sort of IDE (e.g. primalscript) may be
of use to you.

I don't want an option explicit or anything else that slows JScript to the
sort of crawl that VBScript goes along at.

Jim.


Jan Bares

unread,
Dec 9, 1999, 3:00:00 AM12/9/99
to
Do you really think that option explicit will slow down something? Are you
sure that JScript is quicker than VBScript? I am in doubt of both items. And
if it really is, than someone who implemented VBScript must be stupid.

--

Jan Bares
(remove no.spam from my email address)
JPCAD Graphics Engine developer, surf to http://www.antek.cz
PGP ID: 0x6BFE492A

Jim Ley <J...@jibbering.com> wrote in message
news:elK2JjZQ$GA....@cppssbbsa02.microsoft.com...

Jim Ley

unread,
Dec 9, 1999, 3:00:00 AM12/9/99
to

Jan Bares <jan....@antek.cz.no.spam> wrote in message
news:u3eAg1hQ$GA....@cppssbbsa02.microsoft.com...

> Do you really think that option explicit will slow down something? Are you
> sure that JScript is quicker than VBScript? I am in doubt of both items. And
> if it really is, than someone who implemented VBScript must be stupid.

Try it yourself, you'll see massive speed differences between the two (the
only thing I've seen where VBScript is faster, is with Response.Write() inside
ASP, where VBScript kills javascript by about 200% faster, in clientside, and
other scenarios, JScript kills VBScript by up to 1000%.

Try this.

JScript<BR>
<SCRIPT language="JScript">
for (j=0;j<10;j++) {
x=new Date()
for (i=0;i<90000;i++) {
z=i^10/(i^5+i^7)*i/(i^15)
}
y=new Date()
document.write((y-x)+'<BR>')
}
</SCRIPT>
VBScript<BR>
<SCRIPT language="VBScript">

for k=1 to 10
x= Time
for l=1 to 90000
z=l^10/(l^5+l^7)*l/(l^15)
Next
y=Time
Document.Write(DateDiff("s",x,y) & "<BR>")
Next
</SCRIPT>

Jim.

Jan Bares

unread,
Dec 9, 1999, 3:00:00 AM12/9/99
to
You are right that JScript is faster than VBScript but the difference is not
1000%. Declaring variables speeds-up the execution (as I expected). The
option explicit is really missing. Try my version:

<BODY>
Measuring speed of JScript without variable declaration:<BR>
<SCRIPT language="JScript">
for(j1 = 0; j1 < 3; j1++)
{
x1 = new Date()
for(i1 = 0; i1 < 90000; i1++)
{
z1 = i1^10 / (i1^5 + i1^7) * i1 / (i1^15)
}
y1 = new Date()
document.write((y1.getTime() - x1.getTime())/1000 + 's<BR>')
}
</SCRIPT>
<BR>Measuring speed of JScript with variable declaration:<BR>
<SCRIPT language="JScript">
var i2, j2, x2, y2, z2;
for(j2 = 0; j2 < 3; j2++)
{
x2 = new Date()
for(i2 = 0; i2 < 90000; i2++)
{
z2 = i2^10 / (i2^5 + i2^7) * i2 / (i2^15)
}
y2 = new Date()
document.write((y2.getTime() - x2.getTime())/1000 + 's<BR>')
}
</SCRIPT>
<BR>Measuring speed of VBScript without variable declaration:<BR>
<SCRIPT language="VBScript">

for i3 = 1 to 3
x3 = Timer
for j3 = 1 to 90000
z3 = j3^10 / (j3^5 + j3^7) * j3 / (j3^15)
Next
y3 = Timer
Document.Write(y3 - x3 & "s<BR>")
Next
</SCRIPT>
<BR>Measuring speed of VBScript with variable declaration:<BR>
<SCRIPT language="VBScript">

Option Explicit

Dim i4, j4, x4, y4, z4

for i4 = 1 to 3
x4 = Timer
for j4 = 1 to 90000
z4 = j4^10 / (j4^5 + j4^7) * j4 / (j4^15)
Next
y4 = Timer
Document.Write(y4 - x4 & "s<BR>")
Next
</SCRIPT>
</BODY>

Jim Ley

unread,
Dec 9, 1999, 3:00:00 AM12/9/99
to

Jan Bares <jan....@antek.cz.no.spam> wrote in message
news:#S0#WylQ$GA....@cppssbbsa02.microsoft.com...

> You are right that JScript is faster than VBScript but the difference is not
> 1000%. Declaring variables speeds-up the execution (as I expected). The
> option explicit is really missing. Try my version:

I'm not sure using timers in VBScript, and the Date object in JScript, is
completely fair, the Date object is going to take longer to create, but that
is likely insignifigcant here, certainly the 1000% is in a few extreme cases,
VBScript tends to be very slow walking the DOM in IE compared to JScript IME -
obviously that takes longer to give a good testbed.

JScript doesn't seem to care whether they are declared or not for me at any
rate, - 90% of both tests were hitting 1131 or 1132 milliseconds on my machine
(10% of both were in general higher, as the machine did have other processes
which occasionally would've grabbed processor time, I would assume.) So I'd
say the declaration is completely pointless.

VBScript versions were 2.63 +/- 5% and 1.9 +/- 5% so declaring in VBScript,
makes a big difference.

Oh yeah, tests were done on PII at 300Mhz NT4 Srv pack 5, IE5, 5.5 script
engines.

Jim.

Eric Lippert (Microsoft Scripting Dev)

unread,
Dec 9, 1999, 3:00:00 AM12/9/99
to
We're considering extensions to the language that would trap this common
source of bugs. If you have any suggestions, we're happy to entertain them.

I'd like to take this opportunity to point something out though -- "Option
Explicit" in VBScript makes your program run FASTER, not slower, because it
forces you to declare local variables. Declared locals are referenced many
times faster than undeclared locals. Consider the following:

<script language=vbscript>
Sub Foo
x = 10
End Sub
Foo
</script>

Here's what VBScript (and JScript, for that matter) does when running Foo.
First it looks up x in the local name table. It's not there. Then it looks
up x in the global name table. It's not there. Then it checks to see if
there's an object on the page called "x". There isn't. Then it checks to
see if the "window" object has a member called "x". This causes the window
object to ask all the other script engines on the page if they have a global
called "x", so more name tables are searched. Finally, these all fail so
VBScript creates a new entry in the local name table called "x" and fills it
in with 10.

Now consider this:


<script language=vbscript>
Option Explicit
Sub Foo
Dim x
x = 10
End Sub
Foo
</script>

Now VBScript doesn't need to look up ANYTHING. The COMPILER knows that x is
a local, because you've declared it. The code emitted binds directly to the
memory location associated with "x" when assigning in "10". It's thousands
of times faster in some cases. By forcing you to declare locals, Option
Explicit makes you write faster code.

I'm concerned by your assertion that VBScript "crawls", compared to JScript.
I've done extensive performance analysis and the RUN time of VBScript should
be almost identical to JScript. Before version 5, the STARTUP time of
VBScript was much slower than JScript due to the way that VBScript binds
events to event handlers -- the process of searching the name space for
possible event handlers was quite inefficient in VBScript and we
dramatically improved it in v5.

If you have a scenario which is much faster in JScript than VBScript, I
really want to see it. Please post it or send it to me ASAP.

Thanks!

Eric


"Jim Ley" <J...@jibbering.com> wrote in message
news:elK2JjZQ$GA....@cppssbbsa02.microsoft.com...
>

> Jan Bares <jan....@antek.cz.no.spam> wrote in message

Jim Ley

unread,
Dec 10, 1999, 3:00:00 AM12/10/99
to

Eric Lippert (Microsoft Scripting Dev) <Eri...@Microsoft.com> wrote in message
news:82pfv4$k...@news.dns.microsoft.com...

> We're considering extensions to the language that would trap this common
> source of bugs. If you have any suggestions, we're happy to entertain them.

I'd rather you kept VBScript for programs that need a certain amount of hand
holding, and left javascript as close to ECMAScript as possible.

> I'd like to take this opportunity to point something out though -- "Option
> Explicit" in VBScript makes your program run FASTER, not slower, because it
> forces you to declare local variables.

No, Option Explicit does nothing to improve speed, declaring variables does,
but having Option Explicit doesn't, How would you propose option explicit
handle this (simplified) case?

eval("x=10")
alert(x)

> I'm concerned by your assertion that VBScript "crawls", compared to JScript.
> I've done extensive performance analysis and the RUN time of VBScript should
> be almost identical to JScript.

I posted an example in this thread, it was a very simple one, yet generated a
significant difference, in speed.

> If you have a scenario which is much faster in JScript than VBScript, I
> really want to see it. Please post it or send it to me ASAP.

As I said see the examples in the thread, it looped 90000 times performing the
calculation
z = i^10 / (i^5 + i^7) * i / (i^15) where i is from 1 - 90001, (I don't
actually regard the test below as completely fair due to the small difference
in time a timer would take against using date object, but as that is likely to
go against JScript and not make much difference.)

<SCRIPT language="JScript">
var i2, j2, x2, y2, z2;

for(j2 = 0; j2 < 5; j2++)
{
x2 = new Date()
for(i2 = 1; i2 < 90001; i2++)


{
z2 = i2^10 / (i2^5 + i2^7) * i2 / (i2^15)
}
y2 = new Date()

document.write(y2 - x2 + 's<BR>')
}
</SCRIPT>


<SCRIPT language="VBScript">
Option Explicit
Dim i4, j4, x4, y4, z4

for i4 = 1 to 5


x4 = Timer
for j4 = 1 to 90000
z4 = j4^10 / (j4^5 + j4^7) * j4 / (j4^15)
Next
y4 = Timer
Document.Write(y4 - x4 & "s<BR>")
Next
</SCRIPT>

Here's my results :
JScript 1122,1122,1111,1122,1121ms
VBScript 1805,1805,1800,1800,1805ms

which by my calculation makes JScript 61% faster than VBScript, which is at
the low end of tests I've done. Put anything you want in the loop, you'll get
similar results, worst results I've seen are with walking the DOM in IE.
Worst results for JScript are in ASP with a Response.Write() where VBScript
turns the tables on JScript being about 50% faster.

Interestingly changing it to 3 loops instead of one e.g.

for j4 = 1 to 30000


z4 = j4^10 / (j4^5 + j4^7) * j4 / (j4^15)
Next

for j4 = 30001 to 60000


z4 = j4^10 / (j4^5 + j4^7) * j4 / (j4^15)
Next

for j4 = 60001 to 90000


z4 = j4^10 / (j4^5 + j4^7) * j4 / (j4^15)
Next

has no effect in JScript speed, but a small increase in VBscript, (down to
1780-1793ms range.)

Jim.

Jan Bares

unread,
Dec 10, 1999, 3:00:00 AM12/10/99
to
Thanks Eric for deep inside view of variables (I expected behaviour like
that). I am C++ programmer using scripts just to automate some tasks. The
possibility to find all mistyped variables is the main reason why I still
favor VBScript with its ugly syntax (for C++ coder). In my opinion this
option can be just a hint for JScript (like #pragma in C) interpreter. The
code must be compatible with previous versions of JScript.

Regards, Jan

0 new messages