Which is Faster? VBScript or JScript on WSH – A Performance Comparison using Sieve of Eratosthenes

in #vbscript9 years ago (edited)

On WSH (Windows Scripting Host) environment, you can use VBScript or JScript (Microsoft’s implementation of Javascript). Both languages are supported and shipped on every Windows versions since Windows 98. On Win 95, users have to manually install WSH to use these two scripting languages.

Both scripting languages are perfect for Windows Administrative Tasks, and they are still good for many many tasks. But are they equally performing? Let’s take a comparison by implementing the Sieve of Eratosthenes’s Prime Finding algorithms.

VBSCRIPT IMPLEMENTATION

Instead of output the whole prime list, we concatenate the numbers into string and output its length, so the timing comparison will not be affected by console echo.

Const N = 1000000 
ReDim Primes(N) ' VBScript holds 1000000+1 elements

Primes(0) = False
Primes(1) = False
For i = 2 To N
    Primes(i) = True
Next

M = Sqr(N)

For j = 2 To M
    If Primes(j) Then
        i = j * j
        While i <= N  ' Filtering out non-prime numbers
            Primes(i) = False
            i = i + j
        Wend
    End If
Next

S = ""
For i = 0 To N
    If Primes(i) Then ' concatenate numbers into strings
        S = S & i & ", "
    End If
Next
 

' Just output the length
WScript.Echo Len(s)

JSCRIPT IMPLEMENTATION

The equivalent JScript implementation:

(function() {
    var N = 1000000 + 1;
    var Primes = new Array(N);
    Primes[0] = false;
    Primes[1] = false;
    for (var i = 2; i < N; i ++) {
        Primes[i] = true;
    }

    var M = Math.sqrt(N);
    for (var j = 2; j <= M; j ++) {
        if (Primes[j]) {
            var i = j * j;
            for (; i < N; i += j) {
                Primes[i] = false;
            }
        }
    }
    var s = "";

    for (var i = 0; i < N; i ++) {
        if (Primes[i]) {
            s += i + ", ";
        }
    }
    WScript.Echo(s.length); // output length of string to validate the result
})();

TIMING COMPARISON

We use the small utility timeit to measure the performance of both implementation by using the same algorithm.

HP@HP-PC D:\
> timeit "cscript.exe primes.js"
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

616966
2015

HP@HP-PC D:\                                              
> timeit "cscript.exe primes.vbs"                          
Microsoft (R) Windows Script Host Version 5.812            
Copyright (C) Microsoft Corporation. All rights reserved.                                                     
616966                                                    
46828                                                      

So, we can see that JScript is a lot faster (20 times faster) than its equivalent VBScript implementation. It does makes sense because the VBScript (latest version is 5.8 on Windows 10) is not updated (no new features added) since 2000.

This is the Microsoft’s Implementation of JScript, not to mention the Google v8 Javascript engine, which is expected to be a lot faster. But the point of this article is to measure two different scripting languages under the same environment, which is Windows Scripting Host.Is VBScript Dead? what do you think? 

This post has been published on my blog as well:  https://codingforspeed.com/which-is-faster-vbscript-or-jscript-on-wsh-a-performance-comparison-using-sieve-of-eratosthenes/

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

VBscript will always remain. You're comparison is about speed, not about functionality. VBscript will always be superior to Jscript.

You are right, VBScript is my favorite, it is a pity that Microsoft stopped updating it.