Javascript is not a toy

Ron Ward whom I work with is slowly teaching me the advantages of working with Javascript. So far I have been utterly amazed at the advantages of doing just about everything in Javascript. I needed someone like Ron to show me real world examples because to be honest I never would have given Javascript the time of day.

Here is a quick example written by Ron which demonstrates the speed at which Javascript can accomplish a programming task that is extremely well suited to C,/C++ type languages.

Prime number generator

C++

Time taken to run: 1.358 seconds

#include <stdio.h>
 #include <stdlib.h>
class Primes{
 public:
 int getPrimeCount() const { return prime_count; }
 int getPrime(int i) const { return primes[i]; }
 void addPrime(int i) { primes[prime_count++]=i; }
 bool isPrimeDivisible(int candidate){
 for(int i=1; i<prime_count; ++i){
 int currentPrime = primes[i];
 if( (candidate % currentPrime)==0 ) return true;
 }
 return false;
 }
 private:
 volatile int prime_count;
 volatile int primes[100000];
 };
int main() {
 printf("C++ 25000th prime\n");
 Primes p;
 int c=1;
 while(p.getPrimeCount()<25000){
 if(!p.isPrimeDivisible(c)){
 p.addPrime(c);
 }
 c++;
 }
 printf("%d\n", p.getPrime(p.getPrimeCount()-1));
 }

PHP

Time taken to run: 135.9 seconds

<?php
 class Primes {
 public $primeCount=0;
 public $primes = array();
function getPrimeCount() { return $this->primeCount; }
 function getPrime($i) { return $this->primes[$i]; }
 function addPrime($p) { $this->primes[$this->primeCount]=$p; $this->primeCount += 1; }
 function isPrimeDivisible($candidate){
 for($i=1; $i<$this->primeCount; $i+=1){
 $currentPrime = $this->primes[$i];
 if(($candidate % $currentPrime)==0) { return true; }
 }
 return false;
 }
 };
function main(){
 echo "PHP 25,000th prime\n";
 $p = new Primes();
 $c = 1;
 while($p->getPrimeCount()<25000){
 if(!$p->isPrimeDivisible($c)){
 $p->addPrime($c);
 }
 $c+=1;
 }
 echo $p->getPrime($p->getPrimeCount()-1);
 echo "\n";
 }
main();

Javascript

Time taken to run: 5.6 seconds

"use strict";
function getPrimes(){
 var primeCount, primes, obj;
 primeCount = 0;
 primes = new Array(100000);
 obj = {};
 obj.getPrimeCount = function() { return primeCount; };
 obj.getPrime = function(i) { return primes[i]; };
 obj.addPrime = function(p) { primes[primeCount]=p; primeCount+=1; };
 obj.isPrimeDivisible = function(candidate){
 var i, currentPrime;
 for(i=1; i<primeCount; i+=1){
 currentPrime = primes[i];
 if((candidate % currentPrime)===0) { return true; }
 }
 return false;
 };
 return obj;
 }
function main(){
 var p, c;
 console.log("JS 25,000th prime");
 p = getPrimes();
 c = 1;
 while(p.getPrimeCount()<25000){
 if(!p.isPrimeDivisible(c)){
 p.addPrime(c);
 //console.log("added prime ", c);
 }
 c++;
 }
 console.log(p.getPrime(p.getPrimeCount()-1));
 }
main();

Javascript with optimized code

Time taken to run: 0.25 seconds 1/4 of a second

"use strict";
function getPrimes(){
 var primeCount, primes, obj;
 primeCount = 0;
 primes = new Array(100000);
 obj = {};
 obj.getPrimeCount = function() { return primeCount; };
 obj.getPrime = function(i) { return primes[i]; };
 obj.addPrime = function(p) { primes[primeCount]=p; primeCount+=1; };
 obj.isPrimeDivisible = function(candidate){
 var i, currentPrime;
 for(i=1; i<primeCount; i+=1){
 currentPrime = primes[i];
 if(currentPrime*currentPrime>candidate) { return false; }
 if((candidate % currentPrime)===0) { return true; }
 }
 return false;
 };
 return obj;
 }
function main(){
 var p, c;
 console.log("JS 25,000th prime");
 p = getPrimes();
 c = 1;
 while(p.getPrimeCount()<25000){
 if(!p.isPrimeDivisible(c)){
 p.addPrime(c);
 //console.log("added prime ", c);
 }
 c++;
 }
 console.log(p.getPrime(p.getPrimeCount()-1));
 }
main();

Now before you say, “Hey that is cheating using optimized code!”, remember that the natural Javascript code ran considerably fast and the real truth is, when in a real world situation not having to wrestle with a poorly designed language like PHP allows you extra time to optimizing the syntax and logic before your time on that project is up.

What are your thoughts?

Advertisements

2 thoughts on “Javascript is not a toy

  1. Hi,
    I tested your code and got some different results. This was on a laptop with a Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz, running Ubuntu 13.04 64 bits and the latest version of V8 from Git and compiled for x64:
    C++ 25000th prime
    287107

    real 0m1.552s
    user 0m1.544s
    sys 0m0.004

    JS “Normal”
    JS 25,000th prime
    287107

    real 0m1.772s
    user 0m1.760s
    sys 0m0.008s

    JS Optimized
    JS 25,000th prime
    287107

    real 0m0.043s
    user 0m0.036s
    sys 0m0.008s

    The only thing I’ve altered was the assignment of the Array because with arrays > 64000 elements, V8 is better optimized for a declaration like var x = [] instead of var x = new Array(100000).

    Javascript really impresses me every day.

    Alex

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s