JScript Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For jscript
Ackermann's Function
function Ack(M, N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

var n;

ARGS = WScript.Arguments;

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}

WScript.Echo("Ack(3,"+ n+ "):", Ack(3, n));

Array Access
// -*- mode: java -*-
// $Id: ary3.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// By David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

var n, i, k;

ARGS = WScript.Arguments

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1000;
}
var x = Array(n);
var y = Array(n);

for (i = 0; i < n; i++) {
  x[i] = i + 1;
  y[i] = 0; // Need to set all entries in i to zero or the result will be NaN 
}
for (k = 0 ; k < 1000; k++) {
  for (i = n-1; i >= 0; i--) {
    y[i] += x[i];
  }
}
WScript.Echo(y[0], y[n-1]);
Count Lines/Words/Chars
// -*- mode: java -*-
// $Id: wc.njs,v 1.1 2001/07/10 02:36:59 doug Exp $
// http://www.bagley.org/~doug/shootout/
// from David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

var nl = 0, nw = 0, nc = 0; // line, word and character counters

var buf, i;
var stdin = WScript.StdIn;
while(!stdin.AtEndOfStream) {
  buf = stdin.readLine();
  if(buf.length > 0) {
    nc += buf.length;
    var words = buf.split(/\s+/);
    nw += words.length;
  }
  nc += 1;
  nl += 1;
  // WScript.Echo(nl, nw, nc);  
} 

WScript.Echo(nl, nw, nc);
Fibonacci Numbers
// -*- mode: java -*-
// $Id: fibo.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// by David Hedbor <david@hedbor.org>
// modified by Aldo Calpini <dada@perl.it> for Win32

function fib(n) {
    if (n < 2) return 1;
    return fib(n-2) + fib(n-1);
}

var n;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}
WScript.Echo(fib(n));
Hash (Associative Array) Access
// -*- mode: java -*-
// $Id: hash.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// from: David Hedbor <david@hedbor.org>
// modified by Aldo Calpini <dada@perl.it> for Win32

var i, c = 0;
var n;

ARGS = WScript.Arguments;

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}
var X = new Object();
for (i=1; i<=n; i++) {
   X[i.toString(16)] = i;
}
for (i=n; i>0; i--) {
  if (X[i.toString()]) c++;
}
WScript.Echo(c);


Hashes, Part II
// -*- mode: java -*-
// $Id: hash2.njs,v 1.1 2001/07/10 02:39:11 doug Exp $
// http://www.bagley.org/~doug/shootout/
// by David Hedbor <david@hedbor.org>
// modified by Aldo Calpini <dada@perl.it> for Win32

var n;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 150;
}

var hash1 = Object();
var hash2 = Object();
var arr = Array(10000);
var idx;

for (i=0; i<10000; i++) {
  idx = "foo_"+i;
  hash1[idx] = i;
  // Do this here and run loop below one less since += on an undefined
  // entry == NaN.
  hash2[idx] = hash1[idx];
}

for (i = 1; i < n; i++) {
  for(a in hash1) {
    hash2[a] += hash1[a];
  }
}

WScript.Echo(hash1["foo_1"], hash1["foo_9999"],
             hash2["foo_1"], hash2["foo_9999"]);

Heapsort
// -*- mode: java -*-
// $Id: heapsort.njs,v 1.1 2001/07/31 17:21:30 doug Exp $
// http://www.bagley.org/~doug/shootout/
// from: David Hedbor

var IM = 139968;
var IA = 3877;
var IC = 29573;

var last = 42;

function gen_random(max) { return(max * (last = (last * IA + IC) % IM) / IM); }

function heapsort(n, ra) {
    var l, j, ir, i;
    var rra;

    l = (n >> 1) + 1;
    ir = n;
    for (;;) {
        if (l > 1) {
            rra = ra[--l];
        } else {
            rra = ra[ir];
            ra[ir] = ra[1];
            if (--ir == 1) {
                ra[1] = rra;
                return;
            }
        }
        i = l;
        j = l << 1;
        while (j <= ir) {
            if (j < ir && ra[j] < ra[j+1]) { ++j; }
            if (rra < ra[j]) {
                ra[i] = ra[j];
                j += (i = j);
            } else {
                j = ir + 1;
            }
        }
        ra[i] = rra;
    }
}

var n;

ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}

var ary, i;
    
// create an array of N random floats
ary = Array(n+1);
for (i=1; i<=n; i++) {
  ary[i] = gen_random(1.0);
}
heapsort(n, ary);
WScript.Echo(ary[n].valueOf().toFixed(10));

Hello World
WScript.Echo( "hello world" );
List Operations
// -*- mode: java -*-
// $Id: lists.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// from: David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

var SIZE = 10000;

function test_lists()
{
  var Li1, Li2, Li3;
  var tmp;
  // create a list of integers from 1 to SIZE.
  Li1 = new Array();
  for(tmp = 1; tmp <= SIZE; tmp++) Li1.push(tmp);
  // copy the list to Li2.
  Li2 = Li1.concat();

  // remove each element from left side of Li2 and append to
  // the right side of Li3 (preserving order)
  Li3 = new Array();

  while( (tmp = Li2.shift()) ) {
    Li3.push(tmp);
  } 

  // Li2 is now empty.
  // Remove each element from right side of Li3 and append to right
  // side of Li2
  while( (tmp = Li3.pop()) ) {
    Li2.push(tmp);
  } 

  // Li2 is now reversed, and Li3 empty.
  // Reverse Li1 in place.
  Li1.reverse();
  if( Li1[0] != SIZE ) return 0;
  // compare Li1 and Li2 for equality, and return the length of the list.
  for(tmp = 0; tmp < SIZE; tmp++)
    if( Li1[tmp] != Li2[tmp] ) return 0;
  return Li1.length;
}

ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}
var resultl

while( n-- )
  result = test_lists();
WScript.Echo(result );


Matrix Multiplication
/* -*- mode: java -*-
 * $Id: matrix.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * From: David Hedbor
 * modified by Aldo Calpini <dada@perl.it> for Win32
 */

var SIZE=30;

function mkmatrix(rows, cols) {
  var i, j, count = 1;
  var m = new Array(rows);
  for (i = 0; i < rows; i++) {
    m[i] = new Array(cols);
    for (j = 0; j < cols; j++) {
      m[i][j] = count++;
    }
  }
  return m;
}

function mmult(rows, cols,  m1, m2, m3) {
  var i, j, k, val;
  for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
      val = 0;
      for (k = 0; k < cols; k++) {
    val += m1[i][k] * m2[k][j];
      }
      m3[i][j] = val;
    }
  }
  return m3;
}

var n, i;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {
  n = 1;
}
var m1 = mkmatrix(SIZE, SIZE);
var m2 = mkmatrix(SIZE, SIZE);
var mm = mkmatrix(SIZE, SIZE);

for (i = 0; i < n; i++) {
  mmult(SIZE, SIZE, m1, m2, mm);
}
WScript.Echo(mm[0][0], mm[2][3], mm[3][2], mm[4][4]);

Method Calls
// -*- mode: java -*-
// $Id: methcall.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// From David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

function Toggle(start_state) {
  this.bool = start_state;
  
  this.value = ToggleValue;
  this.activate = ToggleActivate;
  
}
function ToggleValue () {
  return this.bool;
}
function ToggleActivate () {
  this.bool = !this.bool;
  return this;
}


function NthToggle (start_state, max_counter) {
  this.base = Toggle;
  this.base(start_state);
  this.count_max = max_counter;
  this.count = 0;

  this.activate = NthToggleActivate;

}
NthToggle.prototype = new Toggle;

function NthToggleActivate () {
if (++this.count >= this.count_max) {
  this.bool = !this.bool;
  this.count = 0;
}
return this;
}

var n, i;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {
  n = 1;
}

var val = true;
var toggle = new Toggle(val);
for (i=0; i<n; i++) {
  val = toggle.activate().value();
}
WScript.Echo(toggle.value() ? "true" : "false");

val = true;
var ntoggle = new NthToggle(val, 3);
for (i=0; i<n; i++) {
  val = ntoggle.activate().value();
}
WScript.Echo(ntoggle.value() ? "true" : "false");

Nested Loops
// -*- mode: java -*-
// $Id: nestedloop.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// From David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

var n, x=0;

ARGS = WScript.Arguments;

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {
  n = 1;
}
var a=n;
// Using while() is faster than for()
while(a--) {
  var b=n; while(b--) {
    var c=n; while(c--) {
      var d=n; while(d--) {
    var e=n; while(e--) {
      var f=n; while(f--) {
        x++;
      }
    }
      }
    }
  }
}
WScript.Echo(x);
Random Number Generator
// -*- mode: java -*-
// $Id: random.njs,v 1.4 2001/08/02 20:27:40 doug Exp $
// http://www.bagley.org/~doug/shootout/
// by David Hedbor <david@hedbor.org>

var IM = 139968;
var IA = 3877;
var IC = 29573;
var last = 42;

function gen_random(max) { return(max * (last = (last * IA + IC) % IM) / IM); }

var n, result = 0;
ARGS = WScript.Arguments;
if(ARGS.length > 0) {
    n = parseInt(ARGS.Item(0), "10");
    if (n < 1) n = 1;
} else {
    n = 1;
}
n--;
while (n--) {
    gen_random(100.0);
}

WScript.Echo(gen_random(100.0).toFixed(9));


Sieve of Erathostenes
// -*- mode: java -*-
// $Id: sieve.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// from: David Hedbor
// modified by Aldo Calpini <dada@perl.it> for Win32

var flags, flagsorig = Array(8193);
var n, i, k, count;
  
ARGS = WScript.Arguments;

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}

for (i = 2; i <= 8192; i++) {  flagsorig[i] = 1; }
while (n--) {
  count = 0;
  flags = flagsorig.concat();
  for (i = 2; i <= 8192; i++) {
    if (flags[i]) {
      for (k=i+i; k <= 8192; k+=i)
    flags[k] = 0;
      count++;
    }
  }
}

WScript.Echo("Count:", count);


String Concatenation
// -*- mode: java -*-
// $Id: strcat.njs,v 1.1 2001/07/08 20:20:06 doug Exp $
// http://www.bagley.org/~doug/shootout/
// From: David Hedbor

var n;

ARGS = WScript.Arguments;
if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}
var str = new String("");
while(n--) {  str += "hello\n"; }
WScript.Echo(str.length);